Skip to content

Commit

Permalink
Avoid double lookup when getting resource id
Browse files Browse the repository at this point in the history
  • Loading branch information
sliverc committed Jul 23, 2024
1 parent 928569e commit 6179681
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,11 @@ def get_resource_type_from_serializer(serializer):
def get_resource_id(resource_instance, resource):
"""Returns the resource identifier for a given instance (`id` takes priority over `pk`)."""
if resource and "id" in resource:
return (
encoding.force_str(resource["id"]) if resource["id"] is not None else None
)
_id = resource["id"]
return encoding.force_str(_id) if _id is not None else None
if resource_instance:
return (
encoding.force_str(resource_instance.pk)
if hasattr(resource_instance, "pk") and resource_instance.pk is not None
else None
)
pk = getattr(resource_instance, "pk", None)
return encoding.force_str(pk) if pk is not None else None
return None


Expand Down

0 comments on commit 6179681

Please sign in to comment.