Fixed a flaw in the serializers that prevented OneToOneFields being serialized as JSON objects.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4433 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2007-01-27 13:42:51 +00:00
parent 54feaca70f
commit db8525cc01
2 changed files with 22 additions and 1 deletions

View File

@ -57,7 +57,7 @@ def Deserializer(object_list, **options):
for d in object_list: for d in object_list:
# Look up the model and starting build a dict of data for it. # Look up the model and starting build a dict of data for it.
Model = _get_model(d["model"]) Model = _get_model(d["model"])
data = {Model._meta.pk.name : d["pk"]} data = {Model._meta.pk.attname : d["pk"]}
m2m_data = {} m2m_data = {}
# Handle each field # Handle each field

View File

@ -37,6 +37,13 @@ class Article(models.Model):
def __str__(self): def __str__(self):
return self.headline return self.headline
class AuthorProfile(models.Model):
author = models.OneToOneField(Author)
date_of_birth = models.DateField()
def __str__(self):
return "Profile of %s" % self.author
__test__ = {'API_TESTS':""" __test__ = {'API_TESTS':"""
# Create some data: # Create some data:
>>> from datetime import datetime >>> from datetime import datetime
@ -118,4 +125,18 @@ __test__ = {'API_TESTS':"""
>>> Article.objects.all() >>> Article.objects.all()
[<Article: Just kidding; I love TV poker>, <Article: Time to reform copyright>] [<Article: Just kidding; I love TV poker>, <Article: Time to reform copyright>]
# If you use your own primary key field (such as a OneToOneField),
# it doesn't appear in the serialized field list - it replaces the
# pk identifier.
>>> profile = AuthorProfile(author=joe, date_of_birth=datetime(1970,1,1))
>>> profile.save()
>>> json = serializers.serialize("json", AuthorProfile.objects.all())
>>> json
'[{"pk": "1", "model": "serializers.authorprofile", "fields": {"date_of_birth": "1970-01-01"}}]'
>>> for obj in serializers.deserialize("json", json):
... print obj
<DeserializedObject: Profile of Joe>
"""} """}