summaryrefslogtreecommitdiff
path: root/django/core/serializers/python.py
diff options
context:
space:
mode:
authorChris Beaven <smileychris@gmail.com>2010-12-21 04:40:08 +0000
committerChris Beaven <smileychris@gmail.com>2010-12-21 04:40:08 +0000
commitb60d5df07266dd4cd4f63afa6986ffb2932facc2 (patch)
treea08f75ea3485e70b5b18bde8ea7d44414b78a0bc /django/core/serializers/python.py
parentc2657d892396c51448432637a855eb9ecac5363c (diff)
Fixes #13252 -- Use the natural key instead of the primary key when serializing
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14994 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/serializers/python.py')
-rw-r--r--django/core/serializers/python.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
index a68ea21f78..839fee12a9 100644
--- a/django/core/serializers/python.py
+++ b/django/core/serializers/python.py
@@ -27,11 +27,13 @@ class Serializer(base.Serializer):
self._current = {}
def end_object(self, obj):
- self.objects.append({
- "model" : smart_unicode(obj._meta),
- "pk" : smart_unicode(obj._get_pk_val(), strings_only=True),
- "fields" : self._current
- })
+ data = {
+ "model": smart_unicode(obj._meta),
+ "fields": self._current
+ }
+ if not self.use_natural_keys or not hasattr(obj, 'natural_key'):
+ data['pk'] = smart_unicode(obj._get_pk_val(), strings_only=True)
+ self.objects.append(data)
self._current = None
def handle_field(self, obj, field):
@@ -82,7 +84,9 @@ 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.attname : Model._meta.pk.to_python(d["pk"])}
+ data = {}
+ if 'pk' in d:
+ data[Model._meta.pk.attname] = Model._meta.pk.to_python(d['pk'])
m2m_data = {}
# Handle each field
@@ -127,7 +131,9 @@ def Deserializer(object_list, **options):
else:
data[field.name] = field.to_python(field_value)
- yield base.DeserializedObject(Model(**data), m2m_data)
+ obj = base.build_instance(Model, data, db)
+
+ yield base.DeserializedObject(obj, m2m_data)
def _get_model(model_identifier):
"""