Fix UniqueConstraint validation with conditional fields (#9707) - #10021
Fix UniqueConstraint validation with conditional fields (#9707)#10021majidkhazaei wants to merge 8 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>
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 |
…eferenced_base_fields
|
I find the whole conditional uniqueness stuff confusing and don't have the conceptual clarity in my head atm to give this a solid review (I'm sorry). |
|
Hi @browniebroke, just a quick follow-up on this PR. The checks have been green for a while, and the branch is up to date. Whenever you have a chance, I’d really appreciate a review. If there’s anything I can adjust or clarify, please let me know. Thanks! |
| yield ( | ||
| constraint.fields, | ||
| model._default_manager, | ||
| condition_fields, | ||
| constraint.condition, |
| yield ( | ||
| constraint.fields, | ||
| model._default_manager, | ||
| condition_fields, |
There was a problem hiding this comment.
@majidkhazaei please crosscheck this and other open suggestions
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.