Skip to content

Commit 6179681

Browse files
committed
Avoid double lookup when getting resource id
1 parent 928569e commit 6179681

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

rest_framework_json_api/utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,11 @@ def get_resource_type_from_serializer(serializer):
307307
def get_resource_id(resource_instance, resource):
308308
"""Returns the resource identifier for a given instance (`id` takes priority over `pk`)."""
309309
if resource and "id" in resource:
310-
return (
311-
encoding.force_str(resource["id"]) if resource["id"] is not None else None
312-
)
310+
_id = resource["id"]
311+
return encoding.force_str(_id) if _id is not None else None
313312
if resource_instance:
314-
return (
315-
encoding.force_str(resource_instance.pk)
316-
if hasattr(resource_instance, "pk") and resource_instance.pk is not None
317-
else None
318-
)
313+
pk = getattr(resource_instance, "pk", None)
314+
return encoding.force_str(pk) if pk is not None else None
319315
return None
320316

321317

0 commit comments

Comments
 (0)