tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

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

Open i701 opened this issue 2 years ago • 1 comments

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 avatar Sep 22 '23 03:09 i701

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

i701 avatar Sep 24 '23 08:09 i701