Opened 14 years ago
Closed 12 years ago
#15744 closed Cleanup/optimization (invalid)
serialization docs improvement
Reported by: | choj | Owned by: | gumuz |
---|---|---|---|
Component: | Documentation | Version: | 1.3 |
Severity: | Normal | Keywords: | docs serialization |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
In http://docs.djangoproject.com/en/1.3/topics/serialization/, the code fragment
return (self.first_name, self.last_name)
could be improved as a dict
return {'first name':self.first_name, 'last name':self.last_name}
to show how to enable fieldnames in the JSON output, rather than the default "naturalkey".
Attachments (1)
Change History (8)
comment:1 by , 14 years ago
Easy pickings: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
by , 13 years ago
Attachment: | ticket15744.diff added |
---|
comment:2 by , 13 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
UI/UX: | unset |
comment:3 by , 13 years ago
Has patch: | set |
---|
comment:5 by , 13 years ago
Patch needs improvement: | set |
---|
Just after the example, the doc says:
That method should always return a natural key tuple
I don't know if it's the example or the code but there is a mismatch.
comment:6 by , 13 years ago
Patch needs improvement: | unset |
---|
Hm, there are some problems with natural key dictionaries connected with serialization as well as deserialization.
Consider the following models.py file:
class PersonManager(models.Manager): def get_by_natural_key(self, *args, **kwargs): print args, kwargs # just for debug #return self.get(first_name=first_name, last_name=last_name) # previous version class Person(models.Model): objects = PersonManager() first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) birthdate = models.DateField() def natural_key(self): return {'first name': self.first_name, 'last name': self.last_name} # after applying patch #return (self.first_name, self.last_name) # before applying patch class Meta: unique_together = (('first_name', 'last_name'),) class Book(models.Model): name = models.CharField(max_length=100) author = models.ForeignKey(Person)
First, it works correctly with json
, but not with xml
. Produced xml
by typing serialization.serialize('xml', Book.objects.all(), use_natural_keys=True)
is
<?xml version="1.0" encoding="utf-8"?> <django-objects version="1.0"><object pk="1" model="polls.book"><field type="CharField" name="name">My title</field><field to="polls.person" name="author" rel="ManyToOneRel"><natural>last name</natural><natural>first name</natural></field></object></django-objects>
You can see that the natural key is labels first name
and last name
instead of values!
I don't know if there is any problem with serializing with yaml
.
There are problems with deserialization too. During deserialization PersonManager.get_by_natural_key
is invoked, but args
and kwargs
given to this method are:
# these lines are printed by 3rd line of models.py (u'last name', u'first name') {} # xml ('last name', 'first name') {} # json
I guess that natural key should be tuple and intention of core developers was not using dictonaries. Accidentally json serialization works (that's undocumented feature :-) ). I suggest to close this ticket - django code is correct, doc is compatible, because it says about Person.natural_key
That method should always return a natural key tuple ...
and introduction to natural keys says
A natural key is a tuple of values that ...
Another question is if normal or unicode strings should be given to PersonManager.get_by_natural_key
. Now, the unicode string is given during xml deserialization and normal string is given while json deserialization. Is it a bug?
comment:7 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
Agreed with krzysiumed's analysis. This doesn't look like it's a feature or at least a feature that works well enough to be documented.
diff patch