Skip to content

Commit 2202e26

Browse files
committed
Proper error messages
1 parent 64ce376 commit 2202e26

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

rest_framework_json_api/relations.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
from rest_framework.exceptions import ValidationError
22
from rest_framework.relations import *
3+
from django.utils.translation import ugettext_lazy as _
34

45

56
class HyperlinkedRelatedField(HyperlinkedRelatedField):
67
"""
78
This field exists for the sole purpose of accepting PKs as well as URLs
89
when data is submitted back to the serializer
910
"""
11+
default_error_messages = {
12+
'required': _('This field is required.'),
13+
'no_match': _('Invalid hyperlink - No URL match.'),
14+
'incorrect_match': _('Invalid hyperlink - Incorrect URL match.'),
15+
'does_not_exist': _('Invalid hyperlink - Object does not exist.'),
16+
'incorrect_type': _('Incorrect type. Expected URL string, received {data_type}.'),
17+
'pk_does_not_exist': _('Invalid pk "{pk_value}" - object does not exist.'),
18+
'incorrect_pk_type': _('Incorrect type. Expected pk value, received {data_type}.'),
19+
}
1020

1121
def __init__(self, **kwargs):
1222
self.pk_field = kwargs.pop('pk_field', None)
@@ -22,6 +32,6 @@ def to_internal_value(self, data):
2232
try:
2333
return self.get_queryset().get(pk=data)
2434
except ObjectDoesNotExist:
25-
self.fail('does_not_exist', pk_value=data)
35+
self.fail('pk_does_not_exist', pk_value=data)
2636
except (TypeError, ValueError):
27-
self.fail('incorrect_type', data_type=type(data).__name__)
37+
self.fail('incorrect_pk_type', data_type=type(data).__name__)

0 commit comments

Comments
 (0)