Movatterモバイル変換


[0]ホーム

URL:


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

Handle zero as valid pk/id in get_resource_id util method#1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsAUTHORS
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,7 @@ David Guillot, for Contexte <dguillot@contexte.com>
David Vogt <david.vogt@adfinis-sygroup.ch>
Felix Viernickel <felix@gedankenspieler.org>
Greg Aker <greg@gregaker.net>
Humayun Ahmad <humayunahbh@gmail.com>
Jamie Bliss <astronouth7303@gmail.com>
Jason Housley <housleyjk@gmail.com>
Jeppe Fihl-Pearson <jeppe@tenzer.dk>
Expand Down
6 changes: 6 additions & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Note that in line with[Django REST framework policy](https://www.django-rest-framework.org/topics/release-notes/),
any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.

##[Unreleased]

###Fixed

* Handled zero as a valid ID for resource (regression since 6.1.0)

##[7.0.2] - 2024-06-28

###Fixed
Expand Down
10 changes: 4 additions & 6 deletionsrest_framework_json_api/utils.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -307,13 +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 resource["id"] and encoding.force_str(resource["id"]) or None
_id = resource["id"]
return encoding.force_str(_id) if _id is not None else None
if resource_instance:
return (
hasattr(resource_instance, "pk")
and encoding.force_str(resource_instance.pk)
or None
)
pk = getattr(resource_instance, "pk", None)
return encoding.force_str(pk) if pk is not None else None
return None


Expand Down
7 changes: 7 additions & 0 deletionstests/test_utils.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -403,6 +403,13 @@ class SerializerWithoutResourceName(serializers.Serializer):
(None, {"id":11},"11"),
(object(), {"pk":11},None),
(BasicModel(id=6), {"id":11},"11"),
(BasicModel(id=0),None,"0"),
(None, {"id":0},"0"),
(
BasicModel(id=0),
{"id":0},
"0",
),
],
)
deftest_get_resource_id(resource_instance,resource,expected):
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp