Make exclude_unset work in pydantic models dependencies in form body
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import Depends, Form, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
x: str = Form()
@app.get("/")
async def foo(item: Item = Depends()):
print(item.dict(exclude_unset=True))
Description
Open the browser and call endpoint “/”
It prints {}
Wanted Solution
I would like it not to fill dependency model with nulls, but leave it unset, with a parameter in Depends for backward compatibilty
Wanted Code
from fastapi import Depends, Form, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
x: str = Form()
@app.get("/")
async def foo(item: Item = Depends(leave_unset=True)):
print(item.dict(exclude_unset=True))
Alternatives
Make Form() in fields in models work without Depends() in handler function. Right now it is considered as application/json
Operating System
Windows
Operating System Details
Windows 11
FastAPI Version
0.78.0
Python Version
3.10.6
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
How to exclude Optional unset values from a Pydantic model ...
Have in mind that the most important part to make a parameter optional is the part: = None , as it will use...
Read more >Using UploadFile and Pydantic model in one request #2257
Just make class with required fields (better use dataclass). But imo it's bad practice to upload file and body fields simultaneously. 1
Read more >Settings management - pydantic
Create a clearly-defined, type-hinted application configuration class; Automatically read modifications to the configuration from environment variables ...
Read more >Extra Models - FastAPI
Pydantic models have a .dict() method that returns a dict with the model's data. So, if we create a Pydantic object user_in like:....
Read more >How we validate input data using pydantic
We use pydantic because it is fast, does a lot of the dirty work for us, ... We can start out with the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I know about exclude_none, but that’s not exactly what I wanted. That’s different things
I see. You could use
exclude_none=True
like so:This would print
{}
as the default value isNone
. Not exactly the same you were trying to achieve but maybe might work in your specific use case? It would mean that you would not allownull
values as a valid input in certainForm()
fields.