Skip to content

Commit 848d333

Browse files
committed
Removed OrderedDict from renderers.py
1 parent 8df5424 commit 848d333

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

rest_framework_json_api/renderers.py

+32-44
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Renderers
33
"""
44
import copy
5-
from collections import OrderedDict, defaultdict
5+
from collections import defaultdict
66
from collections.abc import Iterable
77

88
import inflection
@@ -56,7 +56,7 @@ def extract_attributes(cls, fields, resource):
5656
"""
5757
Builds the `attributes` object of the JSON:API resource object.
5858
"""
59-
data = OrderedDict()
59+
data = {}
6060
for field_name, field in iter(fields.items()):
6161
# ID is always provided in the root of JSON:API so remove it from attributes
6262
if field_name == "id":
@@ -89,7 +89,7 @@ def extract_relationships(cls, fields, resource, resource_instance):
8989
# Avoid circular deps
9090
from rest_framework_json_api.relations import ResourceRelatedField
9191

92-
data = OrderedDict()
92+
data = {}
9393

9494
# Don't try to extract relationships from a non-existent resource
9595
if resource_instance is None:
@@ -127,12 +127,10 @@ def extract_relationships(cls, fields, resource, resource_instance):
127127

128128
for related_object in relation_queryset:
129129
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+
}
136134
)
137135

138136
data.update(
@@ -171,18 +169,12 @@ def extract_relationships(cls, fields, resource, resource_instance):
171169
if not resolved:
172170
continue
173171
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+
}
186178

187179
if isinstance(
188180
field, relations.HyperlinkedRelatedField
@@ -233,12 +225,10 @@ def extract_relationships(cls, fields, resource, resource_instance):
233225
)
234226

235227
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+
}
242232
)
243233
data.update(
244234
{
@@ -419,7 +409,7 @@ def extract_meta(cls, serializer, resource):
419409
else:
420410
meta = getattr(serializer, "Meta", None)
421411
meta_fields = getattr(meta, "meta_fields", [])
422-
data = OrderedDict()
412+
data = {}
423413
for field_name in meta_fields:
424414
data.update({field_name: resource.get(field_name)})
425415
return data
@@ -457,37 +447,35 @@ def build_json_resource_obj(
457447
# Determine type from the instance if the underlying model is polymorphic
458448
if force_type_resolution:
459449
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+
}
468458
relationships = cls.extract_relationships(fields, resource, resource_instance)
469459
if relationships:
470-
resource_data.append(("relationships", relationships))
460+
resource_data["relationships"] = relationships
471461
# Add 'self' link if field is present and valid
472462
if api_settings.URL_FIELD_NAME in resource and isinstance(
473463
fields[api_settings.URL_FIELD_NAME], relations.RelatedField
474464
):
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]}
478466

479467
meta = cls.extract_meta(serializer, resource)
480468
if meta:
481-
resource_data.append(("meta", utils.format_field_names(meta)))
469+
resource_data["meta"] = utils.format_field_names(meta)
482470

483-
return OrderedDict(resource_data)
471+
return resource_data
484472

485473
def render_relationship_view(
486474
self, data, accepted_media_type=None, renderer_context=None
487475
):
488476
# Special case for RelationshipView
489477
view = renderer_context.get("view", None)
490-
render_data = OrderedDict([("data", data)])
478+
render_data = {"data": data}
491479
links = view.get_links()
492480
if links:
493481
render_data.update({"links": links}),
@@ -615,7 +603,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
615603
)
616604

617605
# Make sure we render data in a specific order
618-
render_data = OrderedDict()
606+
render_data = {}
619607

620608
if isinstance(data, dict) and data.get("links"):
621609
render_data["links"] = data.get("links")

0 commit comments

Comments
 (0)