Code

Opened 3 years ago

Closed 3 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:

Description

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):
    return self.name

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">
      <natural>h</natural>
      <natural>e</natural>
      <natural>l</natural>
      <natural>l</natural>
      <natural>o</natural>
    </field>
  </object>

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 3 years ago.
a draft

Download all attachments as: .zip

Change History (2)

Changed 3 years ago by anonymous

a draft

comment:1 Changed 3 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

natural_key should always return a tuple, this is documented: http://docs.djangoproject.com/en/1.2/topics/serialization/#serialization-of-natural-keys

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?)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.