Django

Code

Ticket #4714 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

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

Reported by: phil.h.smith@gmail.com Assigned to: pigletto
Milestone: Component: Serialization
Version: SVN Keywords: json serialization primary key None null
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

json_serializer_should_deal_with_None_primary_keys.py (1.0 kB) - added by phil.h.smith@gmail.com on 06/27/07 19:30:24.
reproducing unit test
json.diff (7.8 kB) - added by pigletto on 09/14/07 12:15:59.
Patch against revision 6193

Change History

06/27/07 19:30:24 changed by phil.h.smith@gmail.com

  • attachment json_serializer_should_deal_with_None_primary_keys.py added.

reproducing unit test

07/11/07 21:43:43 changed by Simon G. <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

Great - want to try to fix it?

09/14/07 00:31:12 changed by pigletto

  • owner changed from nobody to pigletto.
  • status changed from new to assigned.

09/14/07 12:15:59 changed by pigletto

  • attachment json.diff added.

Patch against revision 6193

09/14/07 12:21:36 changed by pigletto

  • has_patch set to 1.
  • stage changed from Accepted to 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".

09/15/07 00:32:29 changed by russellm

  • status changed from assigned to closed.
  • resolution set to fixed.

(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.


Add/Change #4714 (JSON serializer doesn't handle None primary keys correctly (has tests.))




Change Properties
Action