Skip to content

Commit 63ff17b

Browse files
committed
Fixes for ResourceRelatedField in the browseable API
1 parent 771ac10 commit 63ff17b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

rest_framework_json_api/relations.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from rest_framework.exceptions import ValidationError
24
from rest_framework.fields import MISSING_ERROR_MESSAGE
35
from 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

Comments
 (0)