|
10 | 10 | from django.utils.translation import ugettext_lazy as _ |
11 | 11 |
|
12 | 12 | from rest_framework.serializers import BaseSerializer, ListSerializer |
13 | | -from rest_framework.relations import RelatedField, HyperlinkedRelatedField |
| 13 | +from rest_framework.relations import RelatedField, HyperlinkedRelatedField, PrimaryKeyRelatedField |
14 | 14 | from rest_framework.settings import api_settings |
15 | 15 | from rest_framework.exceptions import APIException |
16 | 16 |
|
@@ -193,6 +193,34 @@ def extract_relationships(fields, resource): |
193 | 193 | relation_data.append(OrderedDict([('type', relation_type), ('id', extract_id_from_url(link))])) |
194 | 194 |
|
195 | 195 | data.update({field_name: {'data': relation_data}}) |
| 196 | + continue |
| 197 | + |
| 198 | + if isinstance(relation, PrimaryKeyRelatedField): |
| 199 | + for pk in resource[field_name]: |
| 200 | + relation_data.append(OrderedDict([('type', relation_type), ('id', encoding.force_text(pk))])) |
| 201 | + |
| 202 | + data.update({field_name: {'data': relation_data}}) |
| 203 | + continue |
| 204 | + |
| 205 | + if isinstance(field, ListSerializer): |
| 206 | + relation_data = list() |
| 207 | + |
| 208 | + serializer = field.child |
| 209 | + model = serializer.Meta.model |
| 210 | + relation_type = inflection.pluralize(model.__name__).lower() |
| 211 | + |
| 212 | + # Get the serializer fields |
| 213 | + serializer_fields = get_serializer_fields(serializer) |
| 214 | + serializer_data = resource[field_name] |
| 215 | + if isinstance(serializer_data, list): |
| 216 | + for serializer_resource in serializer_data: |
| 217 | + relation_data.append( |
| 218 | + OrderedDict([ |
| 219 | + ('type', relation_type), ('id', extract_id(serializer_fields, serializer_resource)) |
| 220 | + ])) |
| 221 | + |
| 222 | + data.update({field_name: {'data': relation_data}}) |
| 223 | + continue |
196 | 224 |
|
197 | 225 | return format_keys(data) |
198 | 226 |
|
|
0 commit comments