tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.39k stars 359 forks source link

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

Open i701 opened 9 months ago

i701 commented 9 months ago

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.

i701 commented 9 months ago

i found a workaround for this for now. If anyone find this helpful, please star my gist. https://gist.github.com/i701/d35ced6c0f7d1d495c7dc385d87179fb