-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Fix UniqueTogetherValidator
to handle fields w/ source
attr
#9688
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
Fix UniqueTogetherValidator
to handle fields w/ source
attr
#9688
Conversation
UniqueConstraint
validation w/ source
attributeUniqueTogetherValidator
to handle fields w/ source
attr
Alright, I have a fix in for the test. I tried to keep the fix localized and only change what is needed to fix the test, so if I need to restructure or extract some of the logic out for clarity, just let me know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
rest_framework/validators.py:191
- The updated mapping now relies on serializer.fields[field_name].source being present in attrs. Consider ensuring that this key exists or handling the potential absence to avoid another KeyError.
condition_kwargs = {
@auvipy @browniebroke Alright, I've got that change in. Is there anything else needed for this PR? I noticed there's no CHANGELOG file and that it seems to be tracked in either |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks better now
you don't have to worry about it. thanks a lot for your work on this PR |
@auvipy @browniebroke Thanks for y'all's help and reviews on this! 💚 |
This pull request introduces a failing test case to demonstrate a
KeyError
encountered during validation when aModelSerializer
field uses thesource
attribute and the corresponding model field is part of aUniqueConstraint
.When a
ModelSerializer
field specifies asource
attribute to map to a model field with a different name (e.g.,serializer_field = serializers.CharField(source="model_field_name")
), andmodel_field_name
is included in aUniqueConstraint
, the validation process incorrectly attempts to access the validated data using the serializer field name (serializer_field
) instead of the model field name (model_field_name
) specified bysource
.This leads to a
KeyError
within the validator, as the serializer field name is not present in the dictionary being checked at that stage. The relevant part of the traceback is:Note: I just added the test case at the very end of the class purely so I could quickly get a reproduction of this issue. Open to suggestions regarding its placement or structure if a different organization is preferred!