mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-25 19:43:47 +03:00
Fix UniqueTogetherValidator
to handle fields w/ source
attr (#9688)
* Add failing test for `UniqueConstraint` validation with `source` attribute * Fix `UniqueTogetherValidator` to handle fields with source attribute * split inner sources logic out to tuple comprehension
This commit is contained in:
parent
07d3e1ec8f
commit
543996711d
|
@ -188,7 +188,8 @@ class UniqueTogetherValidator:
|
||||||
if attrs[field_name] != getattr(serializer.instance, field_name)
|
if attrs[field_name] != getattr(serializer.instance, field_name)
|
||||||
]
|
]
|
||||||
|
|
||||||
condition_kwargs = {source: attrs[source] for source in self.condition_fields}
|
condition_sources = (serializer.fields[field_name].source for field_name in self.condition_fields)
|
||||||
|
condition_kwargs = {source: attrs[source] for source in condition_sources}
|
||||||
if checked_values and None not in checked_values and qs_exists_with_condition(queryset, self.condition, condition_kwargs):
|
if checked_values and None not in checked_values and qs_exists_with_condition(queryset, self.condition, condition_kwargs):
|
||||||
field_names = ', '.join(self.fields)
|
field_names = ', '.join(self.fields)
|
||||||
message = self.message.format(field_names=field_names)
|
message = self.message.format(field_names=field_names)
|
||||||
|
|
|
@ -683,6 +683,24 @@ class TestUniqueConstraintValidation(TestCase):
|
||||||
result = serializer.save()
|
result = serializer.save()
|
||||||
self.assertIsInstance(result, UniqueConstraintNullableModel)
|
self.assertIsInstance(result, UniqueConstraintNullableModel)
|
||||||
|
|
||||||
|
def test_unique_constraint_source(self):
|
||||||
|
class SourceUniqueConstraintSerializer(serializers.ModelSerializer):
|
||||||
|
raceName = serializers.CharField(source="race_name")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = UniqueConstraintModel
|
||||||
|
fields = ("raceName", "position", "global_id", "fancy_conditions")
|
||||||
|
|
||||||
|
serializer = SourceUniqueConstraintSerializer(
|
||||||
|
data={
|
||||||
|
"raceName": "example",
|
||||||
|
"position": 5,
|
||||||
|
"global_id": 11,
|
||||||
|
"fancy_conditions": 11,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assert serializer.is_valid()
|
||||||
|
|
||||||
|
|
||||||
# Tests for `UniqueForDateValidator`
|
# Tests for `UniqueForDateValidator`
|
||||||
# ----------------------------------
|
# ----------------------------------
|
||||||
|
|
Loading…
Reference in New Issue
Block a user