11from rest_framework .exceptions import ValidationError
22from rest_framework .relations import *
3+ from django .utils .translation import ugettext_lazy as _
34
45
56class 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