Ticket #17602: 17602.diff

File 17602.diff, 3.9 KB (added by Grzegorz Nosek, 13 years ago)
  • django/core/serializers/python.py

    diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
    index a68ea21..6cdfc9f 100644
    a b class Serializer(base.Serializer):  
    4545            self._current[field.name] = field.value_to_string(obj)
    4646
    4747    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
    6054
    6155    def handle_m2m_field(self, obj, field):
    6256        if field.rel.through._meta.auto_created:
  • django/core/serializers/xml_serializer.py

    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):  
    8585        differently from regular fields).
    8686        """
    8787        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)
    9192                # If related object has a natural key, use it
    9293                related = related.natural_key()
    9394                # Iterable natural keys are rolled out as subelements
    class Serializer(base.Serializer):  
    9697                    self.xml.characters(smart_unicode(key_value))
    9798                    self.xml.endElement("natural")
    9899            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))
    106101        else:
    107102            self.xml.addQuickElement("None")
    108103        self.xml.endElement("field")
  • tests/modeltests/serializers/tests.py

    diff --git a/tests/modeltests/serializers/tests.py b/tests/modeltests/serializers/tests.py
    index 721ca09..309a83e 100644
    a b class SerializersTestBase(object):  
    178178        mv_obj = obj_list[0].object
    179179        self.assertEqual(mv_obj.title, movie_title)
    180180
     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
    181194    def test_serialize_with_null_pk(self):
    182195        """
    183196        Tests that serialized data with no primary key results
Back to Top