-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
tests/test_validators.py
Outdated
@@ -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): |
There was a problem hiding this comment.
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?
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
It also possible to unify code related to django-rest-framework/rest_framework/serializers.py Lines 1600 to 1604 in 33d59fe
django-rest-framework/rest_framework/serializers.py Lines 1475 to 1478 in 33d59fe
|
While building validator unique together fields were compared with declared field names instead of model field names
fixes #9700
closes #9710