sqlmodel

sqlmodel: adding an example to a Field()


In pydantic we can add an example to Fields, such as:

Field(default=None, example="A very nice Item")

Which will then be displayed in the swagger API docs:

https://fastapi.tiangolo.com/img/tutorial/body-fields/image01.png

How do we do this in sqlmodel? Adding the key example to the Field class in sqlmodel returns error:

Field() got an unexpected keyword argument 'example'

Thank you!


Solution

  • Can be accomplished using schema_extra:

    Field(default=None, schema_extra={'example': "A very nice Item"})

    Can be found in code:

    https://github.com/tiangolo/sqlmodel/blob/267cd42fb6c17b43a8edb738da1b689af6909300/sqlmodel/main.py#L119