Ticket #25050: deferred-field.patch

File deferred-field.patch, 2.4 KB (added by cecedille1, 9 years ago)

Patch

  • django/core/serializers/python.py

    diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
    index c97ab62..55c6292 100644
    a b class Serializer(base.Serializer):  
    3737        self._current = None
    3838
    3939    def get_dump_object(self, obj):
    40         data = OrderedDict([('model', force_text(obj._meta))])
     40        model = obj._meta.proxy_for_model if obj._deferred else obj.__class__
     41        data = OrderedDict([('model', force_text(model._meta))])
    4142        if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'):
    4243            data["pk"] = force_text(obj._get_pk_val(), strings_only=True)
    4344        data['fields'] = self._current
  • django/core/serializers/xml_serializer.py

    diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py
    index 9a4bd77..9dfd8e7 100644
    a b class Serializer(base.Serializer):  
    5252            raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj))
    5353
    5454        self.indent(1)
    55         attrs = OrderedDict([("model", smart_text(obj._meta))])
     55        model = obj._meta.proxy_for_model if obj._deferred else obj.__class__
     56        attrs = OrderedDict([("model", smart_text(model._meta))])
    5657        if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'):
    5758            obj_pk = obj._get_pk_val()
    5859            if obj_pk is not None:
  • tests/serializers/tests.py

    diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py
    index 505e599..bd688d9 100644
    a b class SerializersTestBase(object):  
    224224                                                serial_str))
    225225        self.assertEqual(deserial_objs[0].object.score, Approximate(3.4, places=1))
    226226
     227    def test_deferred_field_serialization(self):
     228        author = Author.objects.create(name='Victor Hugo')
     229        author = Author.objects.defer('name').get(pk=author.pk)
     230        serial_str = serializers.serialize(self.serializer_name, [author])
     231        deserial_objs = list(serializers.deserialize(self.serializer_name, serial_str))
     232        self.assertEqual(deserial_objs[0].object.__class__, Author)
     233
    227234    def test_custom_field_serialization(self):
    228235        """Tests that custom fields serialize and deserialize intact"""
    229236        team_str = "Spartak Moskva"
Back to Top