mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-03 20:03:35 +03:00
Revert validation change
This commit is contained in:
parent
8e304e1adb
commit
a676d5b86a
|
@ -160,19 +160,10 @@ class UniqueTogetherValidator:
|
||||||
queryset = self.exclude_current_instance(attrs, queryset, serializer.instance)
|
queryset = self.exclude_current_instance(attrs, queryset, serializer.instance)
|
||||||
|
|
||||||
# Ignore validation if any field is None
|
# Ignore validation if any field is None
|
||||||
if serializer.instance is None:
|
|
||||||
checked_values = [
|
checked_values = [
|
||||||
value for field, value in attrs.items() if field in self.fields
|
value for field, value in attrs.items() if field in self.fields
|
||||||
]
|
]
|
||||||
else:
|
if None not in checked_values and qs_exists(queryset):
|
||||||
# Ignore validation if all field values are unchanged
|
|
||||||
checked_values = [
|
|
||||||
value
|
|
||||||
for field, value in attrs.items()
|
|
||||||
if field in self.fields and value != getattr(serializer.instance, field)
|
|
||||||
]
|
|
||||||
|
|
||||||
if checked_values and None not in checked_values and qs_exists(queryset):
|
|
||||||
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)
|
||||||
raise ValidationError(message, code='unique')
|
raise ValidationError(message, code='unique')
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from django import VERSION as django_version
|
from django import VERSION as django_version
|
||||||
|
@ -453,22 +453,6 @@ class TestUniquenessTogetherValidation(TestCase):
|
||||||
serializer = NullUniquenessTogetherSerializer(data=data)
|
serializer = NullUniquenessTogetherSerializer(data=data)
|
||||||
assert not serializer.is_valid()
|
assert not serializer.is_valid()
|
||||||
|
|
||||||
def test_ignore_validation_for_unchanged_fields(self):
|
|
||||||
"""
|
|
||||||
If all fields in the unique together constraint are unchanged,
|
|
||||||
then the instance should skip uniqueness validation.
|
|
||||||
"""
|
|
||||||
instance = UniquenessTogetherModel.objects.create(
|
|
||||||
race_name="Paris Marathon", position=1
|
|
||||||
)
|
|
||||||
data = {"race_name": "Paris Marathon", "position": 1}
|
|
||||||
serializer = UniquenessTogetherSerializer(data=data, instance=instance)
|
|
||||||
with patch(
|
|
||||||
"rest_framework.validators.qs_exists"
|
|
||||||
) as mock:
|
|
||||||
assert serializer.is_valid()
|
|
||||||
assert not mock.called
|
|
||||||
|
|
||||||
def test_filter_queryset_do_not_skip_existing_attribute(self):
|
def test_filter_queryset_do_not_skip_existing_attribute(self):
|
||||||
"""
|
"""
|
||||||
filter_queryset should add value from existing instance attribute
|
filter_queryset should add value from existing instance attribute
|
||||||
|
|
Loading…
Reference in New Issue
Block a user