Fix UniqueConstraint validation with conditional fields (#9707) - #10021
Open
majidkhazaei wants to merge 6 commits into
Open
Fix UniqueConstraint validation with conditional fields (#9707)#10021majidkhazaei wants to merge 6 commits into
majidkhazaei wants to merge 6 commits into
Conversation
…ogether validator
- Add helper function for extracting fields from Q objects - Move test methods inside TestUniquenessTogetherValidation class - All 67 tests passing Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes DRF’s ModelSerializer uniqueness validation for Django UniqueConstraint objects whose condition references additional model fields, ensuring DRF uses serializer-level UniqueTogetherValidator (with condition-awareness) instead of an incorrect field-level UniqueValidator.
Changes:
- Add
get_referenced_base_fields_from_q()compatibility helper and use it to detect fields referenced byUniqueConstraint.condition. - Update uniqueness validator selection so single-field constraints with distinct condition fields are validated via
UniqueTogetherValidator. - Extend validator test coverage and document the
UniqueConstraint-with-conditions behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
rest_framework/compat.py |
Adds helper to extract referenced base fields from Q conditions. |
rest_framework/utils/field_mapping.py |
Skips field-level UniqueValidator when condition references additional fields. |
rest_framework/serializers.py |
Treats certain single-field conditional UniqueConstraints as “unique-together” for serializer-level validation. |
tests/test_validators.py |
Adds/adjusts tests for conditional-field uniqueness behavior and expected validator placement. |
docs/api-guide/validators.md |
Documents how DRF handles UniqueConstraint conditions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| ] | ||
|
|
||
|
|
||
| ## Updating nested serializers |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is a rebased and conflict-resolved version of PR #9744.
Changes:
get_referenced_base_fields_from_qhelper torest_framework/compat.pytests/test_validators.pyare passingResolves #9707
Supersedes #9744 (with resolved conflicts)
When using Django's
UniqueConstraintwith conditions that reference other fields,DRF now correctly applies
UniqueTogetherValidatorinstead ofUniqueValidator.