-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
coerce_numbers_to_str needs a per-field variant #8383
Comments
Seems like a reasonable feature request 👍. I think we should still stick with |
Seems perfectly reasonable. I'm a Pydantic newbie thrown in the deep end by migrating a project from v1 to v2, so will quicly admit not being fully up to speed with the API. |
No worries. Let us know if there's anything else you need help with in terms of your migration, or if you notice anything missing from the migration guide. |
Hey @sydney-runkle, I was looking into this issue and I think I can work on this. I looked at the |
Sounds great! Go for it - I think you have the right approach :). Maybe we stick with |
@sydney-runkle I opened a PR in core to support this - pydantic/pydantic-core#1249 When that will get merged I'll start working on a change in pydantic, which should be pretty straight forward |
@sydney-runkle |
Hmm, I think here would probably be the most appropriate location:
That function is used during the schema generation process :). You can also ensure that said setting is only specified for types with which it's compatible. |
@sydney-runkle Thanks! I've opened a PR #9137, but it can't be merged until a new version of core (with support for this) is released and the version is updated in pydantic. |
Brilliant, thanks! |
Initial Checks
Description
We are migrating our code from Pydantic 1 to 2, and came across a situation where we do want some fields to coerce numbers to strings, but other fields in the model should not be coerced. As far as I can tell from the docs,
coerce_numbers_to_str
is an all-or-nothing thing, and will affect the entire model. Being able to specify fields would be nice. Maybe something like:I don't know if that's the best syntax, but that would serve our purposes.
If there is a way to work around this feature not being there, could it be added to https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.coerce_numbers_to_str ?
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: