diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
index a68ea21..d9d4040 100644
a
|
b
|
class Serializer(base.Serializer):
|
27 | 27 | self._current = {} |
28 | 28 | |
29 | 29 | def end_object(self, obj): |
| 30 | if self.use_natural_keys and hasattr(obj, 'natural_key'): |
| 31 | pk = obj.natural_key() |
| 32 | else: |
| 33 | pk = smart_unicode(obj._get_pk_val(), strings_only=True) |
30 | 34 | self.objects.append({ |
31 | | "model" : smart_unicode(obj._meta), |
32 | | "pk" : smart_unicode(obj._get_pk_val(), strings_only=True), |
33 | | "fields" : self._current |
| 35 | "model": smart_unicode(obj._meta), |
| 36 | "pk": pk, |
| 37 | "fields": self._current |
34 | 38 | }) |
35 | 39 | self._current = None |
36 | 40 | |
… |
… |
def Deserializer(object_list, **options):
|
82 | 86 | for d in object_list: |
83 | 87 | # Look up the model and starting build a dict of data for it. |
84 | 88 | Model = _get_model(d["model"]) |
85 | | data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])} |
| 89 | pk = d["pk"] |
| 90 | if hasattr(Model._default_manager, 'get_by_natural_key') and\ |
| 91 | hasattr(pk, '__iter__'): |
| 92 | try: |
| 93 | pk = Model._default_manager.db_manager(db).get_by_natural_key(*pk).pk |
| 94 | except Model.DoesNotExist: |
| 95 | pk = None |
| 96 | else: |
| 97 | pk = Model._meta.pk.to_python(pk) |
| 98 | data = {Model._meta.pk.attname: pk} |
86 | 99 | m2m_data = {} |
87 | 100 | |
88 | 101 | # Handle each field |
diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py
index 5fef3b6..435634d 100644
a
|
b
|
class Serializer(base.Serializer):
|
42 | 42 | raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj)) |
43 | 43 | |
44 | 44 | self.indent(1) |
45 | | self.xml.startElement("object", { |
46 | | "pk" : smart_unicode(obj._get_pk_val()), |
47 | | "model" : smart_unicode(obj._meta), |
48 | | }) |
| 45 | natural_key = self.use_natural_keys and hasattr(obj, 'natural_key') |
| 46 | object_data = {"model": smart_unicode(obj._meta)} |
| 47 | if not natural_key: |
| 48 | object_data['pk'] = smart_unicode(obj._get_pk_val()) |
| 49 | self.xml.startElement("object", object_data) |
| 50 | if natural_key: |
| 51 | for value in obj.natural_key(): |
| 52 | self.indent(2) |
| 53 | self.xml.startElement("natural", {}) |
| 54 | self.xml.characters(smart_unicode(value)) |
| 55 | self.xml.endElement("natural") |
49 | 56 | |
50 | 57 | def end_object(self, obj): |
51 | 58 | """ |
… |
… |
class Deserializer(base.Deserializer):
|
166 | 173 | # bail. |
167 | 174 | Model = self._get_model_from_node(node, "model") |
168 | 175 | |
169 | | # Start building a data dictionary from the object. If the node is |
170 | | # missing the pk attribute, bail. |
171 | | pk = node.getAttribute("pk") |
172 | | if not pk: |
173 | | raise base.DeserializationError("<object> node is missing the 'pk' attribute") |
| 176 | # Start building a data dictionary from the object. |
| 177 | natural_key = False |
| 178 | if hasattr(Model._default_manager, 'get_by_natural_key'): |
| 179 | keys = [n for n in node.childNodes if n.tagName == 'natural'] |
| 180 | if keys: |
| 181 | field_value = [getInnerText(k).strip() for k in keys] |
| 182 | try: |
| 183 | pk = Model._default_manager.db_manager(self.db).get_by_natural_key(*field_value).pk |
| 184 | except Model.DoesNotExist: |
| 185 | pk = None |
| 186 | natural_key = True |
| 187 | if not natural_key: |
| 188 | pk = Model._meta.pk.to_python(node.getAttribute("pk")) |
174 | 189 | |
175 | | data = {Model._meta.pk.attname : Model._meta.pk.to_python(pk)} |
| 190 | data = {Model._meta.pk.attname: pk} |
176 | 191 | |
177 | 192 | # Also start building a dict of m2m data (this is saved as |
178 | 193 | # {m2m_accessor_attribute : [list_of_related_objects]}) |