Fix unchanging field bug when validate assignment true #1232
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
This PR addresses a bug reported here, where if validate_assignment is set to True on a schema, then Pydantic will correctly validate the new values, but the schema object will not persist the new value.
RCA
This is caused by the fact that pydantic-core sets these new values on the object (once validated) directly on the object __ dict __. As it was, this means that the new value will be set onto the DjangoGetter object, rather than the schema object being validated.
Solution
Added an extra condition in the root validator of a Schema object that will call
handler
on the original values before the conversion to a DjangoGetter to ensure the new value is persisted properly on the original object. Comment is also updated to reflect this change.Testing
Added assertions into an existing test that a reassigned value is persisted when validate_assignment = True, and an additional test to test for the lack of ValidationError when validate_assignment is False or None.
Open Questions
This seems odd in the following way: I understand the use of DjangoGetter of course to handle resolvable fields. However, it not behaving like a dictionary is causing some churn where we now have to validate the object twice in certain conditions just to ensure fields are persisted/validated properly. I don't know immediately if there's a better solution, or if there's background here I don't know. Welcome to feedback.