Skip to content

Commit 58e8565

Browse files
committed
fix: add model parents to create unique_constraint_by_fields
1 parent c54c658 commit 58e8565

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

rest_framework/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,9 @@ def get_unique_together_validators(self):
16071607
source_map[source].append(name)
16081608

16091609
unique_constraint_by_fields = {
1610-
constraint.fields: constraint for constraint in self.Meta.model._meta.constraints
1610+
constraint.fields: constraint
1611+
for model_cls in (self.Meta.model, *self.Meta.model._meta.parents)
1612+
for constraint in model_cls._meta.constraints
16111613
if isinstance(constraint, models.UniqueConstraint)
16121614
}
16131615

tests/test_validators.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,13 +627,7 @@ class Meta:
627627
fields=("username", "company_id"),
628628
name="unique_username_company_custom_msg",
629629
violation_error_message="Username must be unique within a company.",
630-
violation_error_code="duplicate_username",
631-
)
632-
if django_version[0] >= 5
633-
else models.UniqueConstraint(
634-
fields=("username", "company_id"),
635-
name="unique_username_company_custom_msg",
636-
violation_error_message="Username must be unique within a company.",
630+
**(dict(violation_error_code="duplicate_username") if django_version[0] >= 5 else {}),
637631
),
638632
models.UniqueConstraint(
639633
fields=("company_id", "role"),

0 commit comments

Comments
 (0)