mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Use model error_messages when available
In the automatically applied UniqueValidator, use the error message from error_messages defined in the model instead of the generic default UniqueValidator message. This fixes #2878.
This commit is contained in:
parent
51443166a8
commit
62c000bc12
|
@ -193,7 +193,15 @@ def get_field_kwargs(field_name, model_field):
|
|||
]
|
||||
|
||||
if getattr(model_field, 'unique', False):
|
||||
validator = UniqueValidator(queryset=model_field.model._default_manager)
|
||||
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.object_name,
|
||||
'field_label': model_field.verbose_name
|
||||
}
|
||||
validator = UniqueValidator(
|
||||
queryset=model_field.model._default_manager,
|
||||
message=unique_error_message)
|
||||
validator_kwarg.append(validator)
|
||||
|
||||
if validator_kwarg:
|
||||
|
|
|
@ -48,7 +48,7 @@ class TestUniquenessValidation(TestCase):
|
|||
data = {'username': 'existing'}
|
||||
serializer = UniquenessSerializer(data=data)
|
||||
assert not serializer.is_valid()
|
||||
assert serializer.errors == {'username': ['This field must be unique.']}
|
||||
assert serializer.errors == {'username': ['UniquenessModel with this username already exists.']}
|
||||
|
||||
def test_is_unique(self):
|
||||
data = {'username': 'other'}
|
||||
|
|
Loading…
Reference in New Issue
Block a user