Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model config setting for SerializeAsAny #7494

Closed
5 of 13 tasks
berzi opened this issue Sep 19, 2023 · 3 comments
Closed
5 of 13 tasks

Model config setting for SerializeAsAny #7494

berzi opened this issue Sep 19, 2023 · 3 comments

Comments

@berzi
Copy link

berzi commented Sep 19, 2023

Initial Checks

  • 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?

Affected Components

@sydney-runkle
Copy link
Member

Hi @berzi,

Thanks for your question. 2 potential solutions:

  1. Create a subclass of ConfigDict where SerializeAsAny is always True, then use that as the config for all of your models.
  2. 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! 😄

@berzi
Copy link
Author

berzi commented Sep 19, 2023

Hi @berzi,

Thanks for your question. 2 potential solutions:

  1. Create a subclass of ConfigDict where SerializeAsAny is always True, then use that as the config for all of your models.
  2. 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.

@sydney-runkle
Copy link
Member

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 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants