Ticket #17717: 17717.diff

File 17717.diff, 3.5 KB (added by akaariai, 4 years ago)
  • django/core/serializers/base.py

    diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
    index c7e6226..e4bba1d 100644
    a b class Serializer(object): 
    4141        self.start_serialization()
    4242        for obj in queryset:
    4343            self.start_object(obj)
    44             for field in obj._meta.local_fields:
     44            # Use the concrete parent class' _meta instead of the object's _meta
     45            # This is to avoid local_fields problems for proxy models. Refs #17717.
     46            concrete_class = obj._meta.proxy_for_model or obj.__class__
     47            for field in concrete_class._meta.local_fields:
    4548                if field.serialize:
    4649                    if field.rel is None:
    4750                        if self.selected_fields is None or field.attname in self.selected_fields:
    class Serializer(object): 
    4952                    else:
    5053                        if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
    5154                            self.handle_fk_field(obj, field)
    52             for field in obj._meta.many_to_many:
     55            for field in concrete_class._meta.many_to_many:
    5356                if field.serialize:
    5457                    if self.selected_fields is None or field.attname in self.selected_fields:
    5558                        self.handle_m2m_field(obj, field)
  • tests/regressiontests/serializers_regress/models.py

    diff --git a/tests/regressiontests/serializers_regress/models.py b/tests/regressiontests/serializers_regress/models.py
    index b3ae1fe..9381349 100644
    a b class ExplicitInheritBaseModel(BaseModel): 
    259259    parent = models.OneToOneField(BaseModel)
    260260    child_data = models.IntegerField()
    261261
     262class ProxyBaseModel(BaseModel):
     263    class Meta:
     264        proxy = True
     265
    262266class LengthModel(models.Model):
    263267    data = models.IntegerField()
    264268
  • tests/regressiontests/serializers_regress/tests.py

    diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
    index 5abff63..d8c5fab 100644
    a b from .models import (BooleanData, CharData, DateData, DateTimeData, EmailData, 
    4040    DecimalPKData, FloatPKData, IntegerPKData, IPAddressPKData,
    4141    GenericIPAddressPKData, PhonePKData, PositiveIntegerPKData,
    4242    PositiveSmallIntegerPKData, SlugPKData, SmallPKData, USStatePKData,
    43     AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel,
    44     ExplicitInheritBaseModel, InheritBaseModel, BigIntegerData, LengthModel,
    45     Tag, ComplexModel)
     43    AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel, BaseModel,
     44    ExplicitInheritBaseModel, InheritBaseModel, ProxyBaseModel, BigIntegerData,
     45    LengthModel, Tag, ComplexModel)
    4646
    4747# A set of functions that can be used to recreate
    4848# test data objects of various kinds.
    class SerializerTests(TestCase): 
    408408            for obj in serializers.deserialize("yaml", "{"):
    409409                pass
    410410
     411    def test_serialize_proxy_model(self):
     412        BaseModel.objects.create(parent_data=1)
     413        base_objects  = BaseModel.objects.all()
     414        proxy_objects = ProxyBaseModel.objects.all()
     415        base_data  = serializers.serialize("json", base_objects)
     416        proxy_data = serializers.serialize("json", proxy_objects)
     417        self.assertEqual(base_data, proxy_data.replace('proxy', ''))
     418
    411419
    412420def serializerTest(format, self):
    413421
Back to Top