Skip to content

Fix regression in unique_together validation with SerializerMethodField #9712

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,12 +1469,13 @@ def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs
model_field.unique_for_year}

unique_constraint_names -= {None}
model_fields_names = set(model_fields.keys())

# Include each of the `unique_together` and `UniqueConstraint` field names,
# so long as all the field names are included on the serializer.
for unique_together_list, queryset, condition_fields, condition in self.get_unique_together_constraints(model):
unique_together_list_and_condition_fields = set(unique_together_list) | set(condition_fields)
if set(field_names).issuperset(unique_together_list_and_condition_fields):
if model_fields_names.issuperset(unique_together_list_and_condition_fields):
unique_constraint_names |= unique_together_list_and_condition_fields

# Now we have all the field names that have uniqueness constraints
Expand Down
37 changes: 37 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,43 @@ def filter(self, **kwargs):
validator.filter_queryset(attrs=data, queryset=queryset, serializer=serializer)
assert queryset.called_with == {'race_name': 'bar', 'position': 1}

def test_uniq_together_validation_uses_model_fields_method_field(self):
class TestSerializer(serializers.ModelSerializer):
position = serializers.SerializerMethodField()

def get_position(self, obj):
return obj.position or 0

class Meta:
model = NullUniquenessTogetherModel
fields = ['race_name', 'position']

serializer = TestSerializer()
expected = dedent("""
TestSerializer():
race_name = CharField(max_length=100)
position = SerializerMethodField()
""")
assert repr(serializer) == expected

def test_uniq_together_validation_uses_model_fields_with_source_field(self):
class TestSerializer(serializers.ModelSerializer):
pos = serializers.IntegerField(source='position')

class Meta:
model = NullUniquenessTogetherModel
fields = ['race_name', 'pos']

serializer = TestSerializer()
expected = dedent("""
TestSerializer():
race_name = CharField(max_length=100, required=True)
pos = IntegerField(source='position')
class Meta:
validators = [<UniqueTogetherValidator(queryset=NullUniquenessTogetherModel.objects.all(), fields=('race_name', 'pos'))>]
""")
assert repr(serializer) == expected


class UniqueConstraintModel(models.Model):
race_name = models.CharField(max_length=100)
Expand Down
Loading