From dc49e6143a1e3cfafa4b7b41e4ab3c86d3e68918 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 3 Mar 2012 17:50:18 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#17717=20--=20Fixed=20serialization=20o?= =?UTF-8?q?f=20proxy=20models.=20Thanks,=20Anssi=20K=C3=A4=C3=A4ri=C3=A4in?= =?UTF-8?q?en.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@17640 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/serializers/base.py | 7 +++++-- .../regressiontests/serializers_regress/models.py | 4 ++++ tests/regressiontests/serializers_regress/tests.py | 14 +++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index c7e62265cf..e4bba1d240 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -41,7 +41,10 @@ class Serializer(object): self.start_serialization() for obj in queryset: self.start_object(obj) - for field in obj._meta.local_fields: + # Use the concrete parent class' _meta instead of the object's _meta + # This is to avoid local_fields problems for proxy models. Refs #17717. + concrete_class = obj._meta.proxy_for_model or obj.__class__ + for field in concrete_class._meta.local_fields: if field.serialize: if field.rel is None: if self.selected_fields is None or field.attname in self.selected_fields: @@ -49,7 +52,7 @@ class Serializer(object): else: if self.selected_fields is None or field.attname[:-3] in self.selected_fields: self.handle_fk_field(obj, field) - for field in obj._meta.many_to_many: + for field in concrete_class._meta.many_to_many: if field.serialize: if self.selected_fields is None or field.attname in self.selected_fields: self.handle_m2m_field(obj, field) diff --git a/tests/regressiontests/serializers_regress/models.py b/tests/regressiontests/serializers_regress/models.py index b3ae1feaa5..9381349a61 100644 --- a/tests/regressiontests/serializers_regress/models.py +++ b/tests/regressiontests/serializers_regress/models.py @@ -259,6 +259,10 @@ class ExplicitInheritBaseModel(BaseModel): parent = models.OneToOneField(BaseModel) child_data = models.IntegerField() +class ProxyBaseModel(BaseModel): + class Meta: + proxy = True + class LengthModel(models.Model): data = models.IntegerField() diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py index 5abff6345f..d8c5faba14 100644 --- a/tests/regressiontests/serializers_regress/tests.py +++ b/tests/regressiontests/serializers_regress/tests.py @@ -40,9 +40,9 @@ from .models import (BooleanData, CharData, DateData, DateTimeData, EmailData, DecimalPKData, FloatPKData, IntegerPKData, IPAddressPKData, GenericIPAddressPKData, PhonePKData, PositiveIntegerPKData, PositiveSmallIntegerPKData, SlugPKData, SmallPKData, USStatePKData, - AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel, - ExplicitInheritBaseModel, InheritBaseModel, BigIntegerData, LengthModel, - Tag, ComplexModel) + AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel, BaseModel, + ExplicitInheritBaseModel, InheritBaseModel, ProxyBaseModel, BigIntegerData, + LengthModel, Tag, ComplexModel) # A set of functions that can be used to recreate # test data objects of various kinds. @@ -408,6 +408,14 @@ class SerializerTests(TestCase): for obj in serializers.deserialize("yaml", "{"): pass + def test_serialize_proxy_model(self): + BaseModel.objects.create(parent_data=1) + base_objects = BaseModel.objects.all() + proxy_objects = ProxyBaseModel.objects.all() + base_data = serializers.serialize("json", base_objects) + proxy_data = serializers.serialize("json", proxy_objects) + self.assertEqual(base_data, proxy_data.replace('proxy', '')) + def serializerTest(format, self):