|
8 | 8 | import inflection |
9 | 9 | from django.db.models import Manager |
10 | 10 | from django.template import loader |
11 | | -from django.utils import encoding |
| 11 | +from django.utils.encoding import force_str |
12 | 12 | from rest_framework import relations, renderers |
13 | 13 | from rest_framework.fields import SkipField, get_attribute |
14 | 14 | from rest_framework.relations import PKOnlyObject |
@@ -125,14 +125,10 @@ def extract_relationships(cls, fields, resource, resource_instance): |
125 | 125 | relation_instance if relation_instance is not None else list() |
126 | 126 | ) |
127 | 127 |
|
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 | + ] |
136 | 132 | data.update( |
137 | 133 | { |
138 | 134 | field_name: { |
@@ -173,7 +169,7 @@ def extract_relationships(cls, fields, resource, resource_instance): |
173 | 169 | if relation_id is not None: |
174 | 170 | relation_data["data"] = { |
175 | 171 | "type": relation_type, |
176 | | - "id": encoding.force_str(relation_id), |
| 172 | + "id": force_str(relation_id), |
177 | 173 | } |
178 | 174 |
|
179 | 175 | if isinstance( |
@@ -227,7 +223,7 @@ def extract_relationships(cls, fields, resource, resource_instance): |
227 | 223 | relation_data.append( |
228 | 224 | { |
229 | 225 | "type": nested_resource_instance_type, |
230 | | - "id": encoding.force_str(nested_resource_instance.pk), |
| 226 | + "id": force_str(nested_resource_instance.pk), |
231 | 227 | } |
232 | 228 | ) |
233 | 229 | data.update( |
@@ -447,9 +443,7 @@ def build_json_resource_obj( |
447 | 443 | # Determine type from the instance if the underlying model is polymorphic |
448 | 444 | if force_type_resolution: |
449 | 445 | 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 |
453 | 447 | resource_data = { |
454 | 448 | "type": resource_name, |
455 | 449 | "id": resource_id, |
|
0 commit comments