-
|
Is your feature request related to a problem? Please describe. Describe the solution you'd like Describe alternatives you've considered Additional context |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
|
For what it’s worth, I believe pydantic 1.0 is aiming to support custom json encoders per model. That may go a long way toward powering this feature (though may still require some changes to fastapi). |
Beta Was this translation helpful? Give feedback.
-
|
@daigok you can also use:
in the path operation decorator, like: @app.get("/", response_model_skip_defaults=None)
def read():
... |
Beta Was this translation helpful? Give feedback.
-
|
👋 I'm not sure if it's the best place but I'm looking at a similar thing. I've got a model with some default values, when adding class TileJSON(BaseModel):
"""
TileJSON model.
Based on https://github.com/mapbox/tilejson-spec/tree/master/2.2.0
"""
tilejson: str = "2.2.0"
name: Optional[str]
description: Optional[str]
version: str = "1.0.0"
attribution: Optional[str]
template: Optional[str]
legend: Optional[str]
scheme: str = "xyz"
tiles: List[str]
grids: Optional[List[str]]
data: Optional[List[str]]
minzoom: int = Field(0, ge=0, le=30)
maxzoom: int = Field(30, ge=0, le=30)
bounds: List[float] = [-180, -90, 180, 90]
center: Tuple[float, float, int]Thanks for pointing me to the right doc of code, and I'm happy to contribute in anyway |
Beta Was this translation helpful? Give feedback.
-
|
@vincentsarago can you explain what you are receiving as input, what you want to get as output, and what you are actually getting as output? |
Beta Was this translation helpful? Give feedback.
-
|
sure @dmontagu Based in the model shared before, here is what I'm doing And here is the output while idealy it should be (with defaults values from the model) I understand that I could use |
Beta Was this translation helpful? Give feedback.
-
|
I see, that's helpful. I think longer term exclude_unset is the way to go here, but will require a little bit of a rework of how serialization happens. In the short term I'm afraid manual handling is probably going to be necessary for this case. One thing that might be convenient, depending on context, would be to override the |
Beta Was this translation helpful? Give feedback.
-
|
👼 thanks for your answer @dmontagu, really appreciate it. I'll see what I can do on my end and try to share the method. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help @dmontagu ! @daigok is your problem solved? May we close this issue? |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo |
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
I see, that's helpful. I think longer term exclude_unset is the way to go here, but will require a little bit of a rework of how serialization happens.
In the short term I'm afraid manual handling is probably going to be necessary for this case.
One thing that might be convenient, depending on context, would be to override the
dictor_iteror similar methods to remove keys with valueNone, if that is your goal. You could do that in a base class and get the logic reused. I believe that would result in FastAPI serializing the model the way you want. Obviously not ideal longer term (and maybe not even workable in the short term depending on how you are using things elsewhere), but might be …