Opened 13 years ago

Closed 13 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: master
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


Seems conceptually similar to #2894.

If you serialize('json', [model]) a model with == 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) (997 bytes) - added by phil.h.smith@… 13 years ago.
reproducing unit test
json.diff (7.8 KB) - added by Maciej Wiśniowski 13 years ago.
Patch against revision 6193

Download all attachments as: .zip

Change History (6)

Changed 13 years ago by phil.h.smith@…

reproducing unit test

comment:1 Changed 13 years ago by Simon G. <dev@…>

Triage Stage: UnreviewedAccepted

Great - want to try to fix it?

comment:2 Changed 13 years ago by Maciej Wiśniowski

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

Changed 13 years ago by Maciej Wiśniowski

Attachment: json.diff added

Patch against revision 6193

comment:3 Changed 13 years ago by Maciej Wiśniowski

Has patch: set
Triage Stage: AcceptedReady for checkin

Patch contains tests but tests won't fail only if 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 Changed 13 years ago by Russell Keith-Magee

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