Fix UniqueTogetherValidator to handle fields w/ source attr#9688
Fix UniqueTogetherValidator to handle fields w/ source attr#9688auvipy merged 3 commits intoencode:masterfrom
UniqueTogetherValidator to handle fields w/ source attr#9688Conversation
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.
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 |
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! 💚 |
|
I am not sure if it is intended but some of my tests started to fail because the validator is ignoring the serializer field "required=False" argument. I fixed by replacing the argument with default=None. |
@rick2ricks This PR is probably not the best place to report this. I'd write up a small reproduction or test case showing this behavior and create a new issue. |
This pull request introduces a failing test case to demonstrate a
KeyErrorencountered during validation when aModelSerializerfield uses thesourceattribute and the corresponding model field is part of aUniqueConstraint.When a
ModelSerializerfield specifies asourceattribute to map to a model field with a different name (e.g.,serializer_field = serializers.CharField(source="model_field_name")), andmodel_field_nameis 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
KeyErrorwithin 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!