Code

Ticket #17717: 17717.diff

File 17717.diff, 3.5 KB (added by akaariai, 2 years ago)
Line 
1diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
2index c7e6226..e4bba1d 100644
3--- a/django/core/serializers/base.py
4+++ b/django/core/serializers/base.py
5@@ -41,7 +41,10 @@ class Serializer(object):
6         self.start_serialization()
7         for obj in queryset:
8             self.start_object(obj)
9-            for field in obj._meta.local_fields:
10+            # Use the concrete parent class' _meta instead of the object's _meta
11+            # This is to avoid local_fields problems for proxy models. Refs #17717.
12+            concrete_class = obj._meta.proxy_for_model or obj.__class__
13+            for field in concrete_class._meta.local_fields:
14                 if field.serialize:
15                     if field.rel is None:
16                         if self.selected_fields is None or field.attname in self.selected_fields:
17@@ -49,7 +52,7 @@ class Serializer(object):
18                     else:
19                         if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
20                             self.handle_fk_field(obj, field)
21-            for field in obj._meta.many_to_many:
22+            for field in concrete_class._meta.many_to_many:
23                 if field.serialize:
24                     if self.selected_fields is None or field.attname in self.selected_fields:
25                         self.handle_m2m_field(obj, field)
26diff --git a/tests/regressiontests/serializers_regress/models.py b/tests/regressiontests/serializers_regress/models.py
27index b3ae1fe..9381349 100644
28--- a/tests/regressiontests/serializers_regress/models.py
29+++ b/tests/regressiontests/serializers_regress/models.py
30@@ -259,6 +259,10 @@ class ExplicitInheritBaseModel(BaseModel):
31     parent = models.OneToOneField(BaseModel)
32     child_data = models.IntegerField()
33 
34+class ProxyBaseModel(BaseModel):
35+    class Meta:
36+        proxy = True
37+
38 class LengthModel(models.Model):
39     data = models.IntegerField()
40 
41diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
42index 5abff63..d8c5fab 100644
43--- a/tests/regressiontests/serializers_regress/tests.py
44+++ b/tests/regressiontests/serializers_regress/tests.py
45@@ -40,9 +40,9 @@ from .models import (BooleanData, CharData, DateData, DateTimeData, EmailData,
46     DecimalPKData, FloatPKData, IntegerPKData, IPAddressPKData,
47     GenericIPAddressPKData, PhonePKData, PositiveIntegerPKData,
48     PositiveSmallIntegerPKData, SlugPKData, SmallPKData, USStatePKData,
49-    AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel,
50-    ExplicitInheritBaseModel, InheritBaseModel, BigIntegerData, LengthModel,
51-    Tag, ComplexModel)
52+    AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel, BaseModel,
53+    ExplicitInheritBaseModel, InheritBaseModel, ProxyBaseModel, BigIntegerData,
54+    LengthModel, Tag, ComplexModel)
55 
56 # A set of functions that can be used to recreate
57 # test data objects of various kinds.
58@@ -408,6 +408,14 @@ class SerializerTests(TestCase):
59             for obj in serializers.deserialize("yaml", "{"):
60                 pass
61 
62+    def test_serialize_proxy_model(self):
63+        BaseModel.objects.create(parent_data=1)
64+        base_objects  = BaseModel.objects.all()
65+        proxy_objects = ProxyBaseModel.objects.all()
66+        base_data  = serializers.serialize("json", base_objects)
67+        proxy_data = serializers.serialize("json", proxy_objects)
68+        self.assertEqual(base_data, proxy_data.replace('proxy', ''))
69+
70 
71 def serializerTest(format, self):
72