Skip to content

Fix unique together validation. #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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kalekseev
Copy link
Contributor

@kalekseev kalekseev commented May 31, 2025

While building validator unique together fields were compared with declared field names instead of model field names

fixes #9700

closes #9710

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me

@@ -516,6 +516,42 @@ 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(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems to cover 2 separate cases, both failing on 3.16. Would you mind spliting it up into 2 methods?

Suggested change
def test_uniq_together_validation_uses_model_fields(self):
def test_uniq_together_validation_uses_model_fields_method_field(self):

serializer = TestSerializer()
expected = dedent("""
TestSerializer():
race_name = CharField(max_length=100)
Copy link
Member

@browniebroke browniebroke Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why race_name is not required here? If I run this test on 3.15.2, I get the following diff (actual with +):

 TestSerializer():
-    race_name = CharField(max_length=100)
+    race_name = CharField(max_length=100, required=True) 

Which is more what I would expect, as the model field is not blank-able:

race_name = models.CharField(max_length=100)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field is still required in the serializer, it's just not explicitly passed as arg thus not presented in the repr. In master it's explicitly set in the wrongly executed code path

uniqueness_extra_kwargs[unique_constraint_name] = {'required': True}

While building validator unique together fields were compared with
declared field names instead of model field names
@kalekseev
Copy link
Contributor Author

It also possible to unify code related to get_unique_together_constraints, these two code blocks are basically the same

for unique_together, queryset, condition_fields, condition in self.get_unique_together_constraints(self.Meta.model):
# Skip if serializer does not map to all unique together sources
unique_together_and_condition_fields = set(unique_together) | set(condition_fields)
if not set(source_map).issuperset(unique_together_and_condition_fields):
continue

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):
unique_constraint_names |= unique_together_list_and_condition_fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SerializerMethodField issue after "unique together validator" fix
3 participants