Skip to content

Instantly share code, notes, and snippets.

@yarik2215
Created April 18, 2022 09:45
Show Gist options
  • Select an option

  • Save yarik2215/62e1ce5205ce87ae170db08322108279 to your computer and use it in GitHub Desktop.

Select an option

Save yarik2215/62e1ce5205ce87ae170db08322108279 to your computer and use it in GitHub Desktop.
Pydantic based form for FastApi
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