mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Fix UniqueTogetherValidator for NULL values
This commit is contained in:
parent
7b639c0cd0
commit
3d85473edf
|
@ -138,7 +138,9 @@ class UniqueTogetherValidator:
|
|||
queryset = self.queryset
|
||||
queryset = self.filter_queryset(attrs, queryset)
|
||||
queryset = self.exclude_current_instance(attrs, queryset)
|
||||
if queryset.exists():
|
||||
|
||||
# Ignore validation if any field is None
|
||||
if None not in attrs.values() and queryset.exists():
|
||||
field_names = ', '.join(self.fields)
|
||||
raise ValidationError(self.message.format(field_names=field_names))
|
||||
|
||||
|
|
|
@ -76,8 +76,8 @@ class TestUniquenessValidation(TestCase):
|
|||
# -----------------------------------
|
||||
|
||||
class UniquenessTogetherModel(models.Model):
|
||||
race_name = models.CharField(max_length=100)
|
||||
position = models.IntegerField()
|
||||
race_name = models.CharField(max_length=100, null=True)
|
||||
position = models.IntegerField(null=True)
|
||||
|
||||
class Meta:
|
||||
unique_together = ('race_name', 'position')
|
||||
|
@ -108,8 +108,8 @@ class TestUniquenessTogetherValidation(TestCase):
|
|||
expected = dedent("""
|
||||
UniquenessTogetherSerializer():
|
||||
id = IntegerField(label='ID', read_only=True)
|
||||
race_name = CharField(max_length=100, required=True)
|
||||
position = IntegerField(required=True)
|
||||
race_name = CharField(allow_null=True, max_length=100, required=True)
|
||||
position = IntegerField(allow_null=True, required=True)
|
||||
class Meta:
|
||||
validators = [<UniqueTogetherValidator(queryset=UniquenessTogetherModel.objects.all(), fields=('race_name', 'position'))>]
|
||||
""")
|
||||
|
@ -178,10 +178,20 @@ class TestUniquenessTogetherValidation(TestCase):
|
|||
expected = dedent("""
|
||||
ExcludedFieldSerializer():
|
||||
id = IntegerField(label='ID', read_only=True)
|
||||
race_name = CharField(max_length=100)
|
||||
race_name = CharField(allow_null=True, max_length=100, required=False)
|
||||
""")
|
||||
assert repr(serializer) == expected
|
||||
|
||||
def test_ignore_validation_for_null_fields(self):
|
||||
UniquenessTogetherModel.objects.create(
|
||||
race_name=None,
|
||||
position=None
|
||||
)
|
||||
data = {'race_name': None, 'position': None}
|
||||
serializer = UniquenessTogetherSerializer(data=data)
|
||||
|
||||
assert serializer.is_valid()
|
||||
|
||||
|
||||
# Tests for `UniqueForDateValidator`
|
||||
# ----------------------------------
|
||||
|
|
Loading…
Reference in New Issue
Block a user