You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@tonytony99 The issue you're encountering is likely due to the utility method get_resource_id treating 0 as a falsy value, which results in it being replaced with None. This is why the id of 0 gets returned as null in the JSON response.
I'd say best to avoid using 0 as a primary key value and start with 1, if there may be cases where you need to support 0 as a valid id then following changes would result in 0 properly rendered in json response.
def get_resource_id(resource_instance, resource):
"""Returns the resource identifier for a given instance (`id` takes priority over `pk`).
"""
# Check explicitly for None to allow 0 as a valid pk
if resource and "id" in resource:
return encoding.force_str(resource["id"]) if resource["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
)
return None
Thanks for reporting. Feel free to work on a PR also containing a reproducing test to ensure we have the right fix and this remains. Potentially this is a regression of 6.1.0 but needs to be verified whether this issue occurs with version 6.0.0.
If a model has an id column that's an integer primary key
and a fixture is loaded where the id has value 0
then the JSON response returned from the API replaces the id of 0 with null
The text was updated successfully, but these errors were encountered: