Opened 7 years ago

Closed 7 years ago

#15488 closed (invalid)

Behaviour natural keys differ between json and xml serializers

Reported by: sueastside@… Owned by: nobody
Component: Core (Serialization) Version: master
Severity: Keywords: serialization, natural key, xml, json
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Behaviour natural keys differ between json and xml serializers

Using a single value for the natural key.

Consider the following code:

Code highlighting:

class A(models.Model):
  name = models.CharField(unique=True) 
  def natural_key(self):

class B(models.Model):
  a = models.ForeignKey(A)

Which results in some nice json:

    "pk": 1, 
    "model": "models.b", 
    "fields": {
      "a": "hello"

but goes wrong in XML:

  <object pk="1" model="models.b">
    <field to="models.a" name="a" rel="ManyToOneRel">

Ofcourse a tuple can be returned from natural_key(), but that results in:

    "pk": 1, 
    "model": "models.b", 
    "fields": {
      "a": ["hello"]

Which is kind of annoying.

Attached is a small patch draft.

Thank you for your time.

Attachments (1)

django.patch (725 bytes) - added by anonymous 7 years ago.
a draft

Download all attachments as: .zip

Change History (2)

Changed 7 years ago by anonymous

Attachment: django.patch added

a draft

comment:1 Changed 7 years ago by Karen Tracey

Resolution: invalid
Status: newclosed

natural_key should always return a tuple, this is documented:

The bug here is that the model's natural_key method is not returning the proper type. The fix is to fix that, not add quiet "fixing up" of the value into Djano.

(What's annoying about the json produced when natural_key returns the right type?)

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