Created
April 18, 2022 09:45
-
-
Save yarik2215/62e1ce5205ce87ae170db08322108279 to your computer and use it in GitHub Desktop.
Pydantic based form for FastApi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fastapi import FastAPI, Form, UploadFile, File, Depends | |
| from pydantic import BaseModel | |
| app = FastAPI() | |
| class FormSerializer(BaseModel): | |
| logo: UploadFile | |
| name: str | |
| class Config: | |
| extra = "ignore" | |
| @classmethod | |
| def as_form(cls, logo: UploadFile = File(...), name: str = Form(...)): | |
| return cls( | |
| logo=logo, | |
| name=name, | |
| ) | |
| @app.post("/create") | |
| def complete_organization_data( # noqa: too-many-locals | |
| organization_data: FormSerializer = Depends(FormSerializer.as_form), | |
| ): | |
| print(organization_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment