Skip to content

Commit fcc7c8d

Browse files
committed
Fix unique together validator doesn't respect condition's fields
1 parent f4194c4 commit fcc7c8d

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

rest_framework/serializers.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -1430,15 +1430,18 @@ def get_unique_together_constraints(self, model):
14301430
"""
14311431
for parent_class in [model] + list(model._meta.parents):
14321432
for unique_together in parent_class._meta.unique_together:
1433-
yield unique_together, model._default_manager
1433+
yield unique_together, model._default_manager, []
14341434
for constraint in parent_class._meta.constraints:
14351435
if isinstance(constraint, models.UniqueConstraint) and len(constraint.fields) > 1:
1436-
yield (
1437-
constraint.fields,
1438-
model._default_manager
1439-
if constraint.condition is None
1440-
else model._default_manager.filter(constraint.condition)
1441-
)
1436+
if constraint.condition is None:
1437+
queryset = model._default_manager
1438+
condition_fields = []
1439+
else:
1440+
queryset = model._default_manager.filter(constraint.condition)
1441+
condition_fields = [
1442+
f[0].split("__")[0] for f in constraint.condition.children
1443+
]
1444+
yield (constraint.fields, queryset, condition_fields)
14421445

14431446
def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs):
14441447
"""
@@ -1470,9 +1473,9 @@ def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs
14701473

14711474
# Include each of the `unique_together` and `UniqueConstraint` field names,
14721475
# so long as all the field names are included on the serializer.
1473-
for unique_together_list, queryset in self.get_unique_together_constraints(model):
1474-
if set(field_names).issuperset(unique_together_list):
1475-
unique_constraint_names |= set(unique_together_list)
1476+
for unique_together_list, queryset, condition_fields in self.get_unique_together_constraints(model):
1477+
if set(field_names).issuperset((*unique_together_list, *condition_fields)):
1478+
unique_constraint_names |= set((*unique_together_list, *condition_fields))
14761479

14771480
# Now we have all the field names that have uniqueness constraints
14781481
# applied, we can add the extra 'required=...' or 'default=...'
@@ -1592,12 +1595,12 @@ def get_unique_together_validators(self):
15921595
# Note that we make sure to check `unique_together` both on the
15931596
# base model class, but also on any parent classes.
15941597
validators = []
1595-
for unique_together, queryset in self.get_unique_together_constraints(self.Meta.model):
1598+
for unique_together, queryset, condition_fields in self.get_unique_together_constraints(self.Meta.model):
15961599
# Skip if serializer does not map to all unique together sources
1597-
if not set(source_map).issuperset(unique_together):
1600+
if not set(source_map).issuperset((*unique_together, *condition_fields)):
15981601
continue
15991602

1600-
for source in unique_together:
1603+
for source in (*unique_together, *condition_fields):
16011604
assert len(source_map[source]) == 1, (
16021605
"Unable to create `UniqueTogetherValidator` for "
16031606
"`{model}.{field}` as `{serializer}` has multiple "
@@ -1614,9 +1617,9 @@ def get_unique_together_validators(self):
16141617
)
16151618

16161619
field_names = tuple(source_map[f][0] for f in unique_together)
1620+
condition_fields = tuple(source_map[f][0] for f in condition_fields)
16171621
validator = UniqueTogetherValidator(
1618-
queryset=queryset,
1619-
fields=field_names
1622+
queryset=queryset, fields=field_names, condition_fields=condition_fields
16201623
)
16211624
validators.append(validator)
16221625
return validators

rest_framework/validators.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ class UniqueTogetherValidator:
9999
missing_message = _('This field is required.')
100100
requires_context = True
101101

102-
def __init__(self, queryset, fields, message=None):
102+
def __init__(self, queryset, fields, message=None, condition_fields=None):
103103
self.queryset = queryset
104104
self.fields = fields
105105
self.message = message or self.message
106+
self.condition_fields = [] if condition_fields is None else condition_fields
106107

107108
def enforce_required_fields(self, attrs, serializer):
108109
"""
@@ -114,7 +115,7 @@ def enforce_required_fields(self, attrs, serializer):
114115

115116
missing_items = {
116117
field_name: self.missing_message
117-
for field_name in self.fields
118+
for field_name in (*self.fields, *self.condition_fields)
118119
if serializer.fields[field_name].source not in attrs
119120
}
120121
if missing_items:
@@ -127,7 +128,7 @@ def filter_queryset(self, attrs, queryset, serializer):
127128
# field names => field sources
128129
sources = [
129130
serializer.fields[field_name].source
130-
for field_name in self.fields
131+
for field_name in (*self.fields, *self.condition_fields)
131132
]
132133

133134
# If this is an update, then any unprovided field should

tests/test_validators.py

+46-2
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ class Meta:
513513
name="unique_constraint_model_together_uniq",
514514
fields=('race_name', 'position'),
515515
condition=models.Q(race_name='example'),
516+
),
517+
models.UniqueConstraint(
518+
name="unique_constraint_model_together_uniq2",
519+
fields=('race_name', 'position'),
520+
condition=models.Q(fancy_conditions__gte=10),
516521
)
517522
]
518523

@@ -563,13 +568,52 @@ def test_unique_together_field(self):
563568
to UniqueTogetherValidator as fields and queryset
564569
"""
565570
serializer = UniqueConstraintSerializer()
566-
assert len(serializer.validators) == 1
571+
assert len(serializer.validators) == 2
567572
validator = serializer.validators[0]
568573
assert validator.fields == ('race_name', 'position')
569574
assert set(validator.queryset.values_list(flat=True)) == set(
570575
UniqueConstraintModel.objects.filter(race_name='example').values_list(flat=True)
571576
)
572577

578+
def test_unique_together_condition(self):
579+
"""
580+
Fields used in UniqueConstraint's condition must be included
581+
into queryset existence check
582+
"""
583+
UniqueConstraintModel.objects.create(
584+
race_name='condition',
585+
position=1,
586+
global_id=10,
587+
fancy_conditions=10
588+
)
589+
serializer = UniqueConstraintSerializer(data={
590+
'race_name': 'condition',
591+
'position': 1,
592+
'global_id': 11,
593+
'fancy_conditions': 11,
594+
})
595+
assert serializer.is_valid()
596+
597+
def test_unique_together_condition_fields_required(self):
598+
"""
599+
Fields used in UniqueConstraint's condition must be present in serializer
600+
"""
601+
serializer = UniqueConstraintSerializer(data={
602+
'race_name': 'condition',
603+
'position': 1,
604+
'global_id': 11,
605+
})
606+
assert not serializer.is_valid()
607+
assert serializer.errors == {'fancy_conditions': ['This field is required.']}
608+
609+
class NoFieldsSerializer(serializers.ModelSerializer):
610+
class Meta:
611+
model = UniqueConstraintModel
612+
fields = ('race_name', 'position', 'global_id')
613+
614+
serializer = NoFieldsSerializer()
615+
assert len(serializer.validators) == 1
616+
573617
def test_single_field_uniq_validators(self):
574618
"""
575619
UniqueConstraint with single field must be transformed into
@@ -579,7 +623,7 @@ def test_single_field_uniq_validators(self):
579623
extra_validators_qty = 2 if django_version[0] >= 5 else 0
580624
#
581625
serializer = UniqueConstraintSerializer()
582-
assert len(serializer.validators) == 1
626+
assert len(serializer.validators) == 2
583627
validators = serializer.fields['global_id'].validators
584628
assert len(validators) == 1 + extra_validators_qty
585629
assert validators[0].queryset == UniqueConstraintModel.objects

0 commit comments

Comments
 (0)