question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make exclude_unset work in pydantic models dependencies in form body

See original GitHub issue

First 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:open
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
drforsecommented, Sep 26, 2022

I see. You could use exclude_none=True like so:

class Item(BaseModel):
    x: str | None = Form()


@app.get("/")
async def foo(item: Item = Depends()):
    print(item.dict(exclude_unset=True, exclude_none=True))

This would print {} as the default value is None. 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 allow null values as a valid input in certain Form() fields.

I know about exclude_none, but that’s not exactly what I wanted. That’s different things

1reaction
JarroVGITcommented, Sep 21, 2022

I see. You could use exclude_none=True like so:

class Item(BaseModel):
    x: str | None = Form()


@app.get("/")
async def foo(item: Item = Depends()):
    print(item.dict(exclude_unset=True, exclude_none=True))

This would print {} as the default value is None. 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 allow null values as a valid input in certain Form() fields.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found