mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Respect model error_messages for relation (#7599)
This commit is contained in:
parent
563a20a040
commit
1396f6886a
|
@ -217,15 +217,9 @@ def get_field_kwargs(field_name, model_field):
|
||||||
]
|
]
|
||||||
|
|
||||||
if getattr(model_field, 'unique', False):
|
if getattr(model_field, 'unique', False):
|
||||||
unique_error_message = model_field.error_messages.get('unique', None)
|
|
||||||
if unique_error_message:
|
|
||||||
unique_error_message = unique_error_message % {
|
|
||||||
'model_name': model_field.model._meta.verbose_name,
|
|
||||||
'field_label': model_field.verbose_name
|
|
||||||
}
|
|
||||||
validator = UniqueValidator(
|
validator = UniqueValidator(
|
||||||
queryset=model_field.model._default_manager,
|
queryset=model_field.model._default_manager,
|
||||||
message=unique_error_message)
|
message=get_unique_error_message(model_field))
|
||||||
validator_kwarg.append(validator)
|
validator_kwarg.append(validator)
|
||||||
|
|
||||||
if validator_kwarg:
|
if validator_kwarg:
|
||||||
|
@ -281,7 +275,9 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
if model_field.validators:
|
if model_field.validators:
|
||||||
kwargs['validators'] = model_field.validators
|
kwargs['validators'] = model_field.validators
|
||||||
if getattr(model_field, 'unique', False):
|
if getattr(model_field, 'unique', False):
|
||||||
validator = UniqueValidator(queryset=model_field.model._default_manager)
|
validator = UniqueValidator(
|
||||||
|
queryset=model_field.model._default_manager,
|
||||||
|
message=get_unique_error_message(model_field))
|
||||||
kwargs['validators'] = kwargs.get('validators', []) + [validator]
|
kwargs['validators'] = kwargs.get('validators', []) + [validator]
|
||||||
if to_many and not model_field.blank:
|
if to_many and not model_field.blank:
|
||||||
kwargs['allow_empty'] = False
|
kwargs['allow_empty'] = False
|
||||||
|
@ -300,3 +296,13 @@ def get_url_kwargs(model_field):
|
||||||
return {
|
return {
|
||||||
'view_name': get_detail_view_name(model_field)
|
'view_name': get_detail_view_name(model_field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_unique_error_message(model_field):
|
||||||
|
unique_error_message = model_field.error_messages.get('unique', None)
|
||||||
|
if unique_error_message:
|
||||||
|
unique_error_message = unique_error_message % {
|
||||||
|
'model_name': model_field.model._meta.verbose_name,
|
||||||
|
'field_label': model_field.verbose_name
|
||||||
|
}
|
||||||
|
return unique_error_message
|
||||||
|
|
|
@ -42,6 +42,12 @@ class RelatedModelSerializer(serializers.ModelSerializer):
|
||||||
fields = ('username', 'email')
|
fields = ('username', 'email')
|
||||||
|
|
||||||
|
|
||||||
|
class RelatedModelUserSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = RelatedModel
|
||||||
|
fields = ('user',)
|
||||||
|
|
||||||
|
|
||||||
class AnotherUniquenessModel(models.Model):
|
class AnotherUniquenessModel(models.Model):
|
||||||
code = models.IntegerField(unique=True)
|
code = models.IntegerField(unique=True)
|
||||||
|
|
||||||
|
@ -83,6 +89,13 @@ class TestUniquenessValidation(TestCase):
|
||||||
assert not serializer.is_valid()
|
assert not serializer.is_valid()
|
||||||
assert serializer.errors == {'username': ['uniqueness model with this username already exists.']}
|
assert serializer.errors == {'username': ['uniqueness model with this username already exists.']}
|
||||||
|
|
||||||
|
def test_relation_is_not_unique(self):
|
||||||
|
RelatedModel.objects.create(user=self.instance)
|
||||||
|
data = {'user': self.instance.pk}
|
||||||
|
serializer = RelatedModelUserSerializer(data=data)
|
||||||
|
assert not serializer.is_valid()
|
||||||
|
assert serializer.errors == {'user': ['related model with this user already exists.']}
|
||||||
|
|
||||||
def test_is_unique(self):
|
def test_is_unique(self):
|
||||||
data = {'username': 'other'}
|
data = {'username': 'other'}
|
||||||
serializer = UniquenessSerializer(data=data)
|
serializer = UniquenessSerializer(data=data)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user