1+ import json
2+
13from rest_framework .exceptions import ValidationError
24from rest_framework .fields import MISSING_ERROR_MESSAGE
35from rest_framework .relations import *
@@ -136,9 +138,11 @@ def get_links(self, obj=None, lookup_field='pk'):
136138 return return_data
137139
138140 def to_internal_value (self , data ):
139- expected_relation_type = get_resource_type_from_queryset (self .queryset )
141+ if isinstance (data , six .text_type ):
142+ data = json .loads (data )
140143 if not isinstance (data , dict ):
141144 self .fail ('incorrect_type' , data_type = type (data ).__name__ )
145+ expected_relation_type = get_resource_type_from_queryset (self .queryset )
142146 if data ['type' ] != expected_relation_type :
143147 self .conflict ('incorrect_relation_type' , relation_type = expected_relation_type , received_type = data ['type' ])
144148 return super (ResourceRelatedField , self ).to_internal_value (data ['id' ])
@@ -151,3 +155,19 @@ def to_representation(self, value):
151155
152156 return OrderedDict ([('type' , format_relation_name (get_resource_type_from_instance (value ))), ('id' , str (pk ))])
153157
158+ @property
159+ def choices (self ):
160+ queryset = self .get_queryset ()
161+ if queryset is None :
162+ # Ensure that field.choices returns something sensible
163+ # even when accessed with a read-only field.
164+ return {}
165+
166+ return OrderedDict ([
167+ (
168+ json .dumps (self .to_representation (item )),
169+ self .display_value (item )
170+ )
171+ for item in queryset
172+ ])
173+
0 commit comments