Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@jezdez jezdez authored
View
18 django/core/serializers/python.py
@@ -45,18 +45,12 @@ def handle_field(self, obj, field):
self._current[field.name] = field.value_to_string(obj)
def handle_fk_field(self, obj, field):
- related = getattr(obj, field.name)
- if related is not None:
- if self.use_natural_keys and hasattr(related, 'natural_key'):
- related = related.natural_key()
- else:
- if field.rel.field_name == related._meta.pk.name:
- # Related to remote object via primary key
- related = related._get_pk_val()
- else:
- # Related to remote object via other field
- related = smart_unicode(getattr(related, field.rel.field_name), strings_only=True)
- self._current[field.name] = related
+ if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'):
+ related = getattr(obj, field.name)
+ value = related.natural_key()
+ else:
+ value = getattr(obj, field.get_attname())
+ self._current[field.name] = value
def handle_m2m_field(self, obj, field):
if field.rel.through._meta.auto_created:
View
15 django/core/serializers/xml_serializer.py
@@ -85,9 +85,10 @@ def handle_fk_field(self, obj, field):
differently from regular fields).
"""
self._start_relational_field(field)
- related = getattr(obj, field.name)
- if related is not None:
- if self.use_natural_keys and hasattr(related, 'natural_key'):
+ related_att = getattr(obj, field.get_attname())
+ if related_att is not None:
+ if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'):
+ related = getattr(obj, field.name)
# If related object has a natural key, use it
related = related.natural_key()
# Iterable natural keys are rolled out as subelements
@@ -96,13 +97,7 @@ def handle_fk_field(self, obj, field):
self.xml.characters(smart_unicode(key_value))
self.xml.endElement("natural")
else:
- if field.rel.field_name == related._meta.pk.name:
- # Related to remote object via primary key
- related = related._get_pk_val()
- else:
- # Related to remote object via other field
- related = getattr(related, field.rel.field_name)
- self.xml.characters(smart_unicode(related))
+ self.xml.characters(smart_unicode(related_att))
else:
self.xml.addQuickElement("None")
self.xml.endElement("field")
View
13 tests/modeltests/serializers/tests.py
@@ -178,6 +178,19 @@ def test_serialize_unicode(self):
mv_obj = obj_list[0].object
self.assertEqual(mv_obj.title, movie_title)
+ def test_serialize_superfluous_queries(self):
+ """Ensure no superfluous queries are made when serializing ForeignKeys
+
+ #17602
+ """
+ ac = Actor(name='Actor name')
+ ac.save()
+ mv = Movie(title='Movie title', actor_id=ac.pk)
+ mv.save()
+
+ with self.assertNumQueries(0):
+ serial_str = serializers.serialize(self.serializer_name, [mv])
+
def test_serialize_with_null_pk(self):
"""
Tests that serialized data with no primary key results
Please sign in to comment.
Something went wrong with that request. Please try again.