mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
Merge pull request #4146 from xiaohanyu/bug-fix-unique-validator-error-with-related-field
Fix #3844, refine validator for fields with <source=> kwargs
This commit is contained in:
commit
2d20f09c0c
|
@ -35,7 +35,7 @@ class UniqueValidator(object):
|
||||||
"""
|
"""
|
||||||
# Determine the underlying model field name. This may not be the
|
# Determine the underlying model field name. This may not be the
|
||||||
# same as the serializer field name if `source=<>` is set.
|
# same as the serializer field name if `source=<>` is set.
|
||||||
self.field_name = serializer_field.source_attrs[0]
|
self.field_name = serializer_field.source_attrs[-1]
|
||||||
# Determine the existing instance, if this is an update operation.
|
# Determine the existing instance, if this is an update operation.
|
||||||
self.instance = getattr(serializer_field.parent, 'instance', None)
|
self.instance = getattr(serializer_field.parent, 'instance', None)
|
||||||
|
|
||||||
|
@ -174,8 +174,8 @@ class BaseUniqueForValidator(object):
|
||||||
"""
|
"""
|
||||||
# Determine the underlying model field names. These may not be the
|
# Determine the underlying model field names. These may not be the
|
||||||
# same as the serializer field names if `source=<>` is set.
|
# same as the serializer field names if `source=<>` is set.
|
||||||
self.field_name = serializer.fields[self.field].source_attrs[0]
|
self.field_name = serializer.fields[self.field].source_attrs[-1]
|
||||||
self.date_field_name = serializer.fields[self.date_field].source_attrs[0]
|
self.date_field_name = serializer.fields[self.date_field].source_attrs[-1]
|
||||||
# Determine the existing instance, if this is an update operation.
|
# Determine the existing instance, if this is an update operation.
|
||||||
self.instance = getattr(serializer, 'instance', None)
|
self.instance = getattr(serializer, 'instance', None)
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ from django.db import models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from rest_framework.validators import UniqueValidator
|
||||||
|
|
||||||
|
|
||||||
def dedent(blocktext):
|
def dedent(blocktext):
|
||||||
|
@ -22,6 +23,20 @@ class UniquenessSerializer(serializers.ModelSerializer):
|
||||||
model = UniquenessModel
|
model = UniquenessModel
|
||||||
|
|
||||||
|
|
||||||
|
class RelatedModel(models.Model):
|
||||||
|
user = models.OneToOneField(UniquenessModel, on_delete=models.CASCADE)
|
||||||
|
email = models.CharField(unique=True, max_length=80)
|
||||||
|
|
||||||
|
|
||||||
|
class RelatedModelSerializer(serializers.ModelSerializer):
|
||||||
|
username = serializers.CharField(source='user.username',
|
||||||
|
validators=[UniqueValidator(queryset=UniquenessModel.objects.all())]) # NOQA
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = RelatedModel
|
||||||
|
fields = ('username', 'email')
|
||||||
|
|
||||||
|
|
||||||
class AnotherUniquenessModel(models.Model):
|
class AnotherUniquenessModel(models.Model):
|
||||||
code = models.IntegerField(unique=True)
|
code = models.IntegerField(unique=True)
|
||||||
|
|
||||||
|
@ -73,6 +88,16 @@ class TestUniquenessValidation(TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
AnotherUniquenessModel._meta.get_field('code').validators, [])
|
AnotherUniquenessModel._meta.get_field('code').validators, [])
|
||||||
|
|
||||||
|
def test_related_model_is_unique(self):
|
||||||
|
data = {'username': 'existing', 'email': 'new-email@example.com'}
|
||||||
|
rs = RelatedModelSerializer(data=data)
|
||||||
|
self.assertFalse(rs.is_valid())
|
||||||
|
self.assertEqual(rs.errors,
|
||||||
|
{'username': ['This field must be unique.']})
|
||||||
|
data = {'username': 'new-username', 'email': 'new-email@example.com'}
|
||||||
|
rs = RelatedModelSerializer(data=data)
|
||||||
|
self.assertTrue(rs.is_valid())
|
||||||
|
|
||||||
|
|
||||||
# Tests for `UniqueTogetherValidator`
|
# Tests for `UniqueTogetherValidator`
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
|
|
Loading…
Reference in New Issue
Block a user