Opened 17 years ago

Closed 17 years ago

#4714 closed (fixed)

JSON serializer doesn't handle None primary keys correctly (has tests.)

Reported by: phil.h.smith@… Owned by: Maciej Wiśniowski
Component: Core (Serialization) Version: dev
Severity: Keywords: json serialization primary key None null
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Seems conceptually similar to #2894.

If you serialize('json', [model]) a model with model.id == None, you will produce json containing "pk": "None" instead of "pk": null. The offending line seems to be in django.core.serializers.python.Serializer.end_object, around line 29, when str(obj._get_pk_val()) is called (or this is intentional and the generated "None" needs to be handled further down the line).

Test cases attached.

Attachments (2)

json_serializer_should_deal_with_None_primary_keys.py (997 bytes ) - added by phil.h.smith@… 17 years ago.
reproducing unit test
json.diff (7.8 KB ) - added by Maciej Wiśniowski 17 years ago.
Patch against revision 6193

Download all attachments as: .zip

Change History (6)

by phil.h.smith@…, 17 years ago

reproducing unit test

comment:1 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedAccepted

Great - want to try to fix it?

comment:2 by Maciej Wiśniowski, 17 years ago

Owner: changed from nobody to Maciej Wiśniowski
Status: newassigned

by Maciej Wiśniowski, 17 years ago

Attachment: json.diff added

Patch against revision 6193

comment:3 by Maciej Wiśniowski, 17 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

Patch contains tests but tests won't fail only if http://code.djangoproject.com/ticket/5448 is already checked in.
In other words, tests from this ticket are dependent of fix from 5448.

Original ticket was only about primary keys issue but I've found that there were some more unconsistences in serializer code.
In general 'pk' was always converted to unicode while primary keys from related fields were taken as is.

I've changed this behaviour that now every string is converted to unicode, while other types stay as they are.
One big deal with this path is that in some cases backward compatiblity may be broken!
It is because now, if you have number in primary key, it will be serialized as a number (in JSON: 'pk: 1). Previously it was always 'pk':"1".

comment:4 by Russell Keith-Magee, 17 years ago

Resolution: fixed
Status: assignedclosed

(In [6264]) Fixed #4714 -- Modified serializers to handle None primary keys correctly. This slightly changes the output format for primary keys on JSON and YAML serializers (PKs will be output as 1, rather than "1". However, fixtures in the old format will still load ok. Thanks for the patch, pigletto.

Note: See TracTickets for help on using tickets.
Back to Top