Replies: 2 comments
-
hi @aleivag 👋 according to your example, u can override model_dump_json method with exclude definitions:
which gives:
and other example:
helpful links: hope it helped |
Beta Was this translation helpful? Give feedback.
-
hi @janas-adam , thanks for the reply... the problem is that the logic for what to exclude its on the Job object not in the actual object that needs to implement them, so if for instance i want to serialize a list of Job i cant do
then |
Beta Was this translation helpful? Give feedback.
-
hi folks. I want to ignore some fields when they have a particular value, when converting a model into json, and others keep them... for instance this code
prints
but i would like it to print
because when those list are huge, those extra bites does count!.
I though of using any of exclude_unset, exclude_defaults, exclude_none from https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump_json, but not all the default should be ignored, and all fields are technically set...
i also played around with overwriting dict but, even though it worked, seems very ugly and not really reliable.
i think my issue is that calling model_dump_json does on a parent model, does not call model_dump_json on the childs... and i can really customize child attributes... maybe there is something that can be done in the json_schema_extra param?
Beta Was this translation helpful? Give feedback.
All reactions