![]() ![]() ![]() You also get error checks for incorrect type operations: In your editor, inside your function you will get type hints and completion everywhere (this wouldn't happen if you received a dict instead of a Pydantic model): The JSON Schemas of your models will be part of your OpenAPI generated schema, and will be shown in the interactive API docs:Īnd will be also used in the API docs inside each path operation that needs them: Those schemas will be part of the generated OpenAPI schema, and used by the automatic documentation UIs.Generate JSON Schema definitions for your model, you can also use them anywhere else you like if it makes sense for your project.As you declared it in the function to be of type Item, you will also have all the editor support (completion, etc) for all of the attributes and their types.Give you the received data in the parameter item.If the data is invalid, it will return a nice and clear error, indicating exactly where and what was the incorrect data.Convert the corresponding types (if needed).With just that Python type declaration, FastAPI will: and declare its type as the model you created, Item. post ( "/items/" ) async def create_item ( item : Item ): return item From typing import Union from fastapi import FastAPI from pydantic import BaseModel class Item ( BaseModel ): name : str description : Union = None price : float tax : Union = None app = FastAPI (). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |