FastAPI shows that you can set response_model_exclude_none=True
in the decorator to leave out fields that have a value of None
: https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter
I would like to do this, but the None
field I want to exclude is nested within the parent response model. I.e.
class InnerResponse(BaseModel):
id: int
name: Optional[str] = None
class Response(BaseModel):
experience: int
prices: List[InnerResponse]
@app.post("/dummy", response_model=apitypes.Response, response_model_exclude_none=True)
async def backend_dummy(payload: apitypes.Request):
...
However, when I get a response back, it the "prices" list here still has InnerResponse
s that have "name": null
.
Is there a way to apply the exclude rule on nested models?
For anyone who finds this while searching: The code above works fine, but my issue was another endpoint outside of this code block that did not have the response_model_exclude_none=True set on it. Every endpoint that needs to exclude those "None" values needs to have that set.