You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched Google & GitHub for similar requests and couldn't find anything
I have read and followed the docs and still think this feature is missing
Description
I understand the reason serialisation was changed from v1 to not serialise as "duck types", and I find the solution of using SerializeAsAny ok, but I have a large project where I always need it wherever I have models as field types for other models.
Would it be possible to add a setting to model_config to imply SerializeAsAny automatically?
Create a subclass of ConfigDict where SerializeAsAny is always True, then use that as the config for all of your models.
Create a subclass of BaseModel with your desired config + use that as the parent class for all of your models for which you need these settings.
Let me know if neither of these work for you and we can figure out a better solution! 😄
ConfigDict doesn't have SerializeAsAny, that's exactly what my proposal is. Right now it can only be applied to individual fields, so there's no way to have as default.
Ah, thanks for following up. I misunderstood your original question. We've had a few requests for this already, and we have an open issue re this discussion: #6423.
I'm going to close this in favor of continuing discussion on the mentioned issue 😃
Initial Checks
Description
I understand the reason serialisation was changed from v1 to not serialise as "duck types", and I find the solution of using
SerializeAsAny
ok, but I have a large project where I always need it wherever I have models as field types for other models.Would it be possible to add a setting to
model_config
to implySerializeAsAny
automatically?Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: