Optional fields in FormModel not working #13077
-
First Check
Commit to Help
Example Codeclass FashionSwapFormData(BaseModel):
PersonImage: Optional[UploadFile] = File(
default=None
),
PersonUrl: Optional[str] = Form(
default=None
),
ClothesImage: Optional[UploadFile] = File(
default=None
),
ClothesUrl: Optional[str]= Form(
default=None
),
#not working
@router_generation.post(
"/fashion_swap/",
response_model_exclude_none=True,
)
async def fashion_swap(
data: Annotated[FashionSwapFormData, Form(media_type="multipart/form-data")],
)
#working
@router_generation.post(
"/fashion_swap/",
response_model_exclude_none=True,
)
async def fashion_swap(
PersonImage: Optional[UploadFile] = File(
default=None
),
PersonUrl: Optional[str] = Form(
default=None
),
ClothesImage: Optional[UploadFile] = File(
default=None
),
ClothesUrl: Optional[str]= Form(
default=None
),
)DescriptionHello, in the code above I do not take into account opional fields in the form model, but if I specify them in the function in the same way, everything works fine For unfilled fields I get the following error
Operating SystemWindows Operating System Detailswindows 11 FastAPI Version0.115.6 Pydantic Version2.10.3 Python Version3.12.7 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
when I copied and pasted your code my ide transformed your model into: class FashionSwapFormData(BaseModel):
PersonImage: Optional[UploadFile] = (File(default=None),)
PersonUrl: Optional[str] = (Form(default=None),)
ClothesImage: Optional[UploadFile] = (File(default=None),)
ClothesUrl: Optional[str] = (Form(default=None),)I had the same error as you But if you remove the comma class FashionSwapFormData(BaseModel):
PersonImage: Optional[UploadFile] = File(default=None)
PersonUrl: Optional[str] = Form(default=None)
ClothesImage: Optional[UploadFile] = File(default=None)
ClothesUrl: Optional[str] = Form(default=None)every thing works fine |
Beta Was this translation helpful? Give feedback.
when I copied and pasted your code my ide transformed your model into:
I had the same error as you
But if you remove the comma
every thing works fine