Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17602 -- Stopped the XML serializer from doing unneeded querie…

…s. Thanks, gnosek.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17439 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 62efdcb037da9df8fb1a3a29cabb517d49288b5d 1 parent 928f605
Jannis Leidel authored February 04, 2012
18  django/core/serializers/python.py
@@ -45,18 +45,12 @@ def handle_field(self, obj, field):
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:
15  django/core/serializers/xml_serializer.py
@@ -85,9 +85,10 @@ def handle_fk_field(self, obj, field):
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
@@ -96,13 +97,7 @@ def handle_fk_field(self, obj, field):
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")
13  tests/modeltests/serializers/tests.py
@@ -178,6 +178,19 @@ def test_serialize_unicode(self):
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

0 notes on commit 62efdcb

Please sign in to comment.
Something went wrong with that request. Please try again.