Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect model error_messages for relation #7599

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions rest_framework/utils/field_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,9 @@ def get_field_kwargs(field_name, model_field):
]

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(
queryset=model_field.model._default_manager,
message=unique_error_message)
message=get_unique_error_message(model_field))
validator_kwarg.append(validator)

if validator_kwarg:
Expand Down Expand Up @@ -281,7 +275,9 @@ def get_relation_kwargs(field_name, relation_info):
if model_field.validators:
kwargs['validators'] = model_field.validators
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]
if to_many and not model_field.blank:
kwargs['allow_empty'] = False
Expand All @@ -300,3 +296,13 @@ def get_url_kwargs(model_field):
return {
'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
13 changes: 13 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class Meta:
fields = ('username', 'email')


class RelatedModelUserSerializer(serializers.ModelSerializer):
class Meta:
model = RelatedModel
fields = ('user',)


class AnotherUniquenessModel(models.Model):
code = models.IntegerField(unique=True)

Expand Down Expand Up @@ -84,6 +90,13 @@ def test_is_not_unique(self):
assert not serializer.is_valid()
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):
data = {'username': 'other'}
serializer = UniquenessSerializer(data=data)
Expand Down