tortoise / tortoise/tortoise-orm

pydantic_model_creator won't expose any fields with fields.DateTime in the schema in the swagger docs.

Open
#1,482 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
I am working on a very simple FastAPI example as suggested by the official documentation here.

In the example you can see usage of DateTime Fields like

created_at = fields.DatetimeField(auto_now_add=True)
modified_at = fields.DatetimeField(auto_now=True)

The problem or bug i noticed is if i use DateField instead of DatetimeField then the schema for the post request to create a User shows up in the FastAPI swagger docs like so

image

But if i use a DatetimeField the schema shows up like this

image

I am aware when creating a pydantic model i can pass the argument of exclude_readonly=True and in the example from the docs that argument is there but this only omits the id of the model in the schema.

I don't understand why even though auto_now_add=True and auto_now=True is specified for the fields it still complains of missing 2 fields. The missing fields error or response shows up like this in the swagger docs.

image

To Reproduce
Create a basic FastAPI App with a main.py file and a models.py file and run the project with same content as the FastAPI example from the documentation

Expected behavior
Expected behavior is for the proper schema with created_at and modified_at to show up in the schema.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pydantic_model_creator entry point and reproduce the behavior using the FastAPI example, with models.py defining DateField and DatetimeField values. Compare the generated schema for the POST request in main.py and trace how created_at and modified_at are handled. Done means the Swagger schema exposes the expected fields without reporting them as missing.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.