|
2 | 2 | Renderers |
3 | 3 | """ |
4 | 4 | import copy |
5 | | -from collections import OrderedDict, defaultdict |
| 5 | +from collections import defaultdict |
6 | 6 | from collections.abc import Iterable |
7 | 7 |
|
8 | 8 | import inflection |
@@ -56,7 +56,7 @@ def extract_attributes(cls, fields, resource): |
56 | 56 | """ |
57 | 57 | Builds the `attributes` object of the JSON:API resource object. |
58 | 58 | """ |
59 | | - data = OrderedDict() |
| 59 | + data = {} |
60 | 60 | for field_name, field in iter(fields.items()): |
61 | 61 | # ID is always provided in the root of JSON:API so remove it from attributes |
62 | 62 | if field_name == "id": |
@@ -89,7 +89,7 @@ def extract_relationships(cls, fields, resource, resource_instance): |
89 | 89 | # Avoid circular deps |
90 | 90 | from rest_framework_json_api.relations import ResourceRelatedField |
91 | 91 |
|
92 | | - data = OrderedDict() |
| 92 | + data = {} |
93 | 93 |
|
94 | 94 | # Don't try to extract relationships from a non-existent resource |
95 | 95 | if resource_instance is None: |
@@ -127,12 +127,10 @@ def extract_relationships(cls, fields, resource, resource_instance): |
127 | 127 |
|
128 | 128 | for related_object in relation_queryset: |
129 | 129 | relation_data.append( |
130 | | - OrderedDict( |
131 | | - [ |
132 | | - ("type", relation_type), |
133 | | - ("id", encoding.force_str(related_object.pk)), |
134 | | - ] |
135 | | - ) |
| 130 | + { |
| 131 | + "type": relation_type, |
| 132 | + "id": encoding.force_str(related_object.pk), |
| 133 | + } |
136 | 134 | ) |
137 | 135 |
|
138 | 136 | data.update( |
@@ -171,18 +169,12 @@ def extract_relationships(cls, fields, resource, resource_instance): |
171 | 169 | if not resolved: |
172 | 170 | continue |
173 | 171 | relation_id = relation if resource.get(field_name) else None |
174 | | - relation_data = { |
175 | | - "data": ( |
176 | | - OrderedDict( |
177 | | - [ |
178 | | - ("type", relation_type), |
179 | | - ("id", encoding.force_str(relation_id)), |
180 | | - ] |
181 | | - ) |
182 | | - if relation_id is not None |
183 | | - else None |
184 | | - ) |
185 | | - } |
| 172 | + relation_data = {"data": None} |
| 173 | + if relation_id is not None: |
| 174 | + relation_data["data"] = { |
| 175 | + "type": relation_type, |
| 176 | + "id": encoding.force_str(relation_id), |
| 177 | + } |
186 | 178 |
|
187 | 179 | if isinstance( |
188 | 180 | field, relations.HyperlinkedRelatedField |
@@ -233,12 +225,10 @@ def extract_relationships(cls, fields, resource, resource_instance): |
233 | 225 | ) |
234 | 226 |
|
235 | 227 | relation_data.append( |
236 | | - OrderedDict( |
237 | | - [ |
238 | | - ("type", nested_resource_instance_type), |
239 | | - ("id", encoding.force_str(nested_resource_instance.pk)), |
240 | | - ] |
241 | | - ) |
| 228 | + { |
| 229 | + "type": nested_resource_instance_type, |
| 230 | + "id": encoding.force_str(nested_resource_instance.pk), |
| 231 | + } |
242 | 232 | ) |
243 | 233 | data.update( |
244 | 234 | { |
@@ -419,7 +409,7 @@ def extract_meta(cls, serializer, resource): |
419 | 409 | else: |
420 | 410 | meta = getattr(serializer, "Meta", None) |
421 | 411 | meta_fields = getattr(meta, "meta_fields", []) |
422 | | - data = OrderedDict() |
| 412 | + data = {} |
423 | 413 | for field_name in meta_fields: |
424 | 414 | data.update({field_name: resource.get(field_name)}) |
425 | 415 | return data |
@@ -457,37 +447,35 @@ def build_json_resource_obj( |
457 | 447 | # Determine type from the instance if the underlying model is polymorphic |
458 | 448 | if force_type_resolution: |
459 | 449 | resource_name = utils.get_resource_type_from_instance(resource_instance) |
460 | | - resource_data = [ |
461 | | - ("type", resource_name), |
462 | | - ( |
463 | | - "id", |
464 | | - encoding.force_str(resource_instance.pk) if resource_instance else None, |
465 | | - ), |
466 | | - ("attributes", cls.extract_attributes(fields, resource)), |
467 | | - ] |
| 450 | + resource_id = ( |
| 451 | + encoding.force_str(resource_instance.pk) if resource_instance else None |
| 452 | + ) |
| 453 | + resource_data = { |
| 454 | + "type": resource_name, |
| 455 | + "id": resource_id, |
| 456 | + "attributes": cls.extract_attributes(fields, resource), |
| 457 | + } |
468 | 458 | relationships = cls.extract_relationships(fields, resource, resource_instance) |
469 | 459 | if relationships: |
470 | | - resource_data.append(("relationships", relationships)) |
| 460 | + resource_data["relationships"] = relationships |
471 | 461 | # Add 'self' link if field is present and valid |
472 | 462 | if api_settings.URL_FIELD_NAME in resource and isinstance( |
473 | 463 | fields[api_settings.URL_FIELD_NAME], relations.RelatedField |
474 | 464 | ): |
475 | | - resource_data.append( |
476 | | - ("links", {"self": resource[api_settings.URL_FIELD_NAME]}) |
477 | | - ) |
| 465 | + resource_data["links"] = {"self": resource[api_settings.URL_FIELD_NAME]} |
478 | 466 |
|
479 | 467 | meta = cls.extract_meta(serializer, resource) |
480 | 468 | if meta: |
481 | | - resource_data.append(("meta", utils.format_field_names(meta))) |
| 469 | + resource_data["meta"] = utils.format_field_names(meta) |
482 | 470 |
|
483 | | - return OrderedDict(resource_data) |
| 471 | + return resource_data |
484 | 472 |
|
485 | 473 | def render_relationship_view( |
486 | 474 | self, data, accepted_media_type=None, renderer_context=None |
487 | 475 | ): |
488 | 476 | # Special case for RelationshipView |
489 | 477 | view = renderer_context.get("view", None) |
490 | | - render_data = OrderedDict([("data", data)]) |
| 478 | + render_data = {"data": data} |
491 | 479 | links = view.get_links() |
492 | 480 | if links: |
493 | 481 | render_data.update({"links": links}), |
@@ -615,7 +603,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None): |
615 | 603 | ) |
616 | 604 |
|
617 | 605 | # Make sure we render data in a specific order |
618 | | - render_data = OrderedDict() |
| 606 | + render_data = {} |
619 | 607 |
|
620 | 608 | if isinstance(data, dict) and data.get("links"): |
621 | 609 | render_data["links"] = data.get("links") |
|
0 commit comments