Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2661f17

Browse files
committed
Simplified creating of dicts in renderers.py
1 parent 8ab5e31 commit 2661f17

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

‎rest_framework_json_api/renderers.py‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import inflection
99
from django.db.models import Manager
1010
from django.template import loader
11-
from django.utils import encoding
11+
from django.utils.encoding import force_str
1212
from rest_framework import relations, renderers
1313
from rest_framework.fields import SkipField, get_attribute
1414
from rest_framework.relations import PKOnlyObject
@@ -125,14 +125,10 @@ def extract_relationships(cls, fields, resource, resource_instance):
125125
relation_instance if relation_instance is not None else list()
126126
)
127127

128-
for related_object in relation_queryset:
129-
relation_data.append(
130-
{
131-
"type": relation_type,
132-
"id": encoding.force_str(related_object.pk),
133-
}
134-
)
135-
128+
relation_data = [
129+
{"type": relation_type, "id": force_str(related_object.pk)}
130+
for related_object in relation_queryset
131+
]
136132
data.update(
137133
{
138134
field_name: {
@@ -173,7 +169,7 @@ def extract_relationships(cls, fields, resource, resource_instance):
173169
if relation_id is not None:
174170
relation_data["data"] = {
175171
"type": relation_type,
176-
"id": encoding.force_str(relation_id),
172+
"id": force_str(relation_id),
177173
}
178174

179175
if isinstance(
@@ -227,7 +223,7 @@ def extract_relationships(cls, fields, resource, resource_instance):
227223
relation_data.append(
228224
{
229225
"type": nested_resource_instance_type,
230-
"id": encoding.force_str(nested_resource_instance.pk),
226+
"id": force_str(nested_resource_instance.pk),
231227
}
232228
)
233229
data.update(
@@ -447,9 +443,7 @@ def build_json_resource_obj(
447443
# Determine type from the instance if the underlying model is polymorphic
448444
if force_type_resolution:
449445
resource_name = utils.get_resource_type_from_instance(resource_instance)
450-
resource_id = (
451-
encoding.force_str(resource_instance.pk) if resource_instance else None
452-
)
446+
resource_id = force_str(resource_instance.pk) if resource_instance else None
453447
resource_data = {
454448
"type": resource_name,
455449
"id": resource_id,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /