Opened 17 years ago
Closed 17 years ago
#4714 closed (fixed)
JSON serializer doesn't handle None primary keys correctly (has tests.)
Reported by: | 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)
Change History (6)
by , 17 years ago
Attachment: | json_serializer_should_deal_with_None_primary_keys.py added |
---|
comment:2 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 17 years ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready 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 , 17 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
(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.
reproducing unit test