Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FixUniqueTogetherValidator 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

joshuadavidthomas
Copy link
Contributor

This pull request introduces a failing test case to demonstrate aKeyError encountered during validation when aModelSerializer field uses thesource attribute and the corresponding model field is part of aUniqueConstraint.

When aModelSerializer 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 aKeyError 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:

tests/test_validators.py:702:intest_unique_constraint_sourceassertserializer.is_valid()rest_framework/serializers.py:225:inis_validself._validated_data=self.run_validation(self.initial_data)rest_framework/serializers.py:446:inrun_validationself.run_validators(value)rest_framework/serializers.py:479:inrun_validatorssuper().run_validators(to_validate)rest_framework/fields.py:551:inrun_validatorsvalidator(value,self)rest_framework/validators.py:191:in__call__condition_kwargs= {source:attrs[source]forsourceinself.condition_fields}EKeyError:'raceName'

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!

browniebroke reacted with thumbs up emoji
@joshuadavidthomasjoshuadavidthomas changed the titleAdd test forUniqueConstraint validation w/source attributeFixUniqueTogetherValidator to handle fields w/source attrApr 14, 2025
@joshuadavidthomas
Copy link
ContributorAuthor

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!

@auvipyauvipy requested review fromauvipy andCopilotApril 16, 2025 04:18
Copy link

@CopilotCopilotAI left a 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 = {

@joshuadavidthomas
Copy link
ContributorAuthor

@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 eitherdocs/community/<version>-announcement.md ordocs/community/release-notes.md, but no place for unreleased changes (unless I'm just obtuse and missing something). If this something I don't need to worry about, that's okay too☺️. Just trying to be a bit pro-active to help out.

Copy link
Member

@auvipyauvipy left a 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

@auvipyauvipy merged commit5439967 intoencode:masterApr 23, 2025
7 checks passed
@auvipy
Copy link
Member

If this something I don't need to worry about, that's okay too☺️. Just trying to be a bit pro-active to help out.

you don't have to worry about it. thanks a lot for your work on this PR

joshuadavidthomas reacted with hooray emojijoshuadavidthomas reacted with heart emoji

@joshuadavidthomas
Copy link
ContributorAuthor

@auvipy@browniebroke Thanks for y'all's help and reviews on this! 💚

jefftriplett reacted with heart emojibrowniebroke and jefftriplett reacted with rocket emoji

@joshuadavidthomasjoshuadavidthomas deleted the unique-constraint-source branchJuly 4, 2025 02:18
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@browniebrokebrowniebrokebrowniebroke left review comments

Copilot code reviewCopilotCopilot left review comments

@auvipyauvipyauvipy approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@joshuadavidthomas@auvipy@browniebroke

[8]ページ先頭

©2009-2025 Movatter.jp