Opened 14 years ago
Closed 14 years ago
#15488 closed (invalid)
Behaviour natural keys differ between json and xml serializers
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Serialization) | Version: | dev |
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: | no | UI/UX: | no |
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)
Change History (2)
by , 14 years ago
Attachment: | django.patch added |
---|
comment:1 by , 14 years ago
Resolution: | → invalid |
---|---|
Status: | new → 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?)
a draft