diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py index 859816c226..1e1e6f4bec 100644 --- a/django/core/serializers/python.py +++ b/django/core/serializers/python.py @@ -57,7 +57,7 @@ def Deserializer(object_list, **options): for d in object_list: # Look up the model and starting build a dict of data for it. Model = _get_model(d["model"]) - data = {Model._meta.pk.name : d["pk"]} + data = {Model._meta.pk.attname : d["pk"]} m2m_data = {} # Handle each field diff --git a/tests/modeltests/serializers/models.py b/tests/modeltests/serializers/models.py index d1d10b43c0..e24ff537c1 100644 --- a/tests/modeltests/serializers/models.py +++ b/tests/modeltests/serializers/models.py @@ -37,6 +37,13 @@ class Article(models.Model): def __str__(self): 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':""" # Create some data: >>> from datetime import datetime @@ -118,4 +125,18 @@ __test__ = {'API_TESTS':""" >>> Article.objects.all() [, ] +# 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 + + """}