diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
index a68ea21..6cdfc9f 100644
a
|
b
|
class Serializer(base.Serializer):
|
45 | 45 | self._current[field.name] = field.value_to_string(obj) |
46 | 46 | |
47 | 47 | def handle_fk_field(self, obj, field): |
48 | | related = getattr(obj, field.name) |
49 | | if related is not None: |
50 | | if self.use_natural_keys and hasattr(related, 'natural_key'): |
51 | | related = related.natural_key() |
52 | | else: |
53 | | if field.rel.field_name == related._meta.pk.name: |
54 | | # Related to remote object via primary key |
55 | | related = related._get_pk_val() |
56 | | else: |
57 | | # Related to remote object via other field |
58 | | related = smart_unicode(getattr(related, field.rel.field_name), strings_only=True) |
59 | | self._current[field.name] = related |
| 48 | if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'): |
| 49 | related = getattr(obj, field.name) |
| 50 | value = related.natural_key() |
| 51 | else: |
| 52 | value = getattr(obj, field.get_attname()) |
| 53 | self._current[field.name] = value |
60 | 54 | |
61 | 55 | def handle_m2m_field(self, obj, field): |
62 | 56 | if field.rel.through._meta.auto_created: |
diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py
index bcf5631..a5edeac 100644
a
|
b
|
class Serializer(base.Serializer):
|
85 | 85 | differently from regular fields). |
86 | 86 | """ |
87 | 87 | self._start_relational_field(field) |
88 | | related = getattr(obj, field.name) |
89 | | if related is not None: |
90 | | if self.use_natural_keys and hasattr(related, 'natural_key'): |
| 88 | related_att = getattr(obj, field.get_attname()) |
| 89 | if related_att is not None: |
| 90 | if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'): |
| 91 | related = getattr(obj, field.name) |
91 | 92 | # If related object has a natural key, use it |
92 | 93 | related = related.natural_key() |
93 | 94 | # Iterable natural keys are rolled out as subelements |
… |
… |
class Serializer(base.Serializer):
|
96 | 97 | self.xml.characters(smart_unicode(key_value)) |
97 | 98 | self.xml.endElement("natural") |
98 | 99 | else: |
99 | | if field.rel.field_name == related._meta.pk.name: |
100 | | # Related to remote object via primary key |
101 | | related = related._get_pk_val() |
102 | | else: |
103 | | # Related to remote object via other field |
104 | | related = getattr(related, field.rel.field_name) |
105 | | self.xml.characters(smart_unicode(related)) |
| 100 | self.xml.characters(smart_unicode(related_att)) |
106 | 101 | else: |
107 | 102 | self.xml.addQuickElement("None") |
108 | 103 | self.xml.endElement("field") |
diff --git a/tests/modeltests/serializers/tests.py b/tests/modeltests/serializers/tests.py
index 721ca09..309a83e 100644
a
|
b
|
class SerializersTestBase(object):
|
178 | 178 | mv_obj = obj_list[0].object |
179 | 179 | self.assertEqual(mv_obj.title, movie_title) |
180 | 180 | |
| 181 | def test_serialize_superfluous_queries(self): |
| 182 | """Ensure no superfluous queries are made when serializing ForeignKeys |
| 183 | |
| 184 | #17602 |
| 185 | """ |
| 186 | ac = Actor(name='Actor name') |
| 187 | ac.save() |
| 188 | mv = Movie(title='Movie title', actor_id=ac.pk) |
| 189 | mv.save() |
| 190 | |
| 191 | with self.assertNumQueries(0): |
| 192 | serial_str = serializers.serialize(self.serializer_name, [mv]) |
| 193 | |
181 | 194 | def test_serialize_with_null_pk(self): |
182 | 195 | """ |
183 | 196 | Tests that serialized data with no primary key results |