diff --git a/rest_framework/validators.py b/rest_framework/validators.py index e3719b8d5..c030abdba 100644 --- a/rest_framework/validators.py +++ b/rest_framework/validators.py @@ -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)) diff --git a/tests/test_validators.py b/tests/test_validators.py index 072cec360..185febf83 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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 = [] """) @@ -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` # ----------------------------------