Ticket #17717: ticket-17717-proxy-model-serializers.diff

File ticket-17717-proxy-model-serializers.diff, 3.6 KB (added by charettes, 3 years ago)

Make sure to rely on newly introduced concrete_model model option

  • django/core/serializers/base.py

    diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
    index e4bba1d..dbe7787 100644
    a b def serialize(self, queryset, **options): 
    4343            self.start_object(obj)
    4444            # Use the concrete parent class' _meta instead of the object's _meta
    4545            # 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:
     46            concrete_model = obj._meta.concrete_model
     47            for field in concrete_model._meta.local_fields:
    4848                if field.serialize:
    4949                    if field.rel is None:
    5050                        if self.selected_fields is None or field.attname in self.selected_fields:
    def serialize(self, queryset, **options): 
    5252                    else:
    5353                        if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
    5454                            self.handle_fk_field(obj, field)
    55             for field in concrete_class._meta.many_to_many:
     55            for field in concrete_model._meta.many_to_many:
    5656                if field.serialize:
    5757                    if self.selected_fields is None or field.attname in self.selected_fields:
    5858                        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 9381349..f366f90 100644
    a b class ProxyBaseModel(BaseModel): 
    263263    class Meta:
    264264        proxy = True
    265265
     266class ProxyProxyBaseModel(ProxyBaseModel):
     267    class Meta:
     268        proxy = True
     269
    266270class LengthModel(models.Model):
    267271    data = models.IntegerField()
    268272
  • tests/regressiontests/serializers_regress/tests.py

    diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
    index d8c5fab..704e34b 100644
    a b  
    4141    GenericIPAddressPKData, PhonePKData, PositiveIntegerPKData,
    4242    PositiveSmallIntegerPKData, SlugPKData, SmallPKData, USStatePKData,
    4343    AutoNowDateTimeData, ModifyingSaveData, InheritAbstractModel, BaseModel,
    44     ExplicitInheritBaseModel, InheritBaseModel, ProxyBaseModel, BigIntegerData,
    45     LengthModel, Tag, ComplexModel)
     44    ExplicitInheritBaseModel, InheritBaseModel, ProxyBaseModel,
     45    ProxyProxyBaseModel, BigIntegerData, LengthModel, Tag, ComplexModel)
    4646
    4747# A set of functions that can be used to recreate
    4848# test data objects of various kinds.
    def test_yaml_deserializer_exception(self): 
    410410
    411411    def test_serialize_proxy_model(self):
    412412        BaseModel.objects.create(parent_data=1)
    413         base_objects  = BaseModel.objects.all()
     413        base_objects = BaseModel.objects.all()
    414414        proxy_objects = ProxyBaseModel.objects.all()
    415         base_data  = serializers.serialize("json", base_objects)
     415        proxy_proxy_objects = ProxyProxyBaseModel.objects.all()
     416        base_data = serializers.serialize("json", base_objects)
    416417        proxy_data = serializers.serialize("json", proxy_objects)
     418        proxy_proxy_data = serializers.serialize("json", proxy_proxy_objects)
    417419        self.assertEqual(base_data, proxy_data.replace('proxy', ''))
     420        self.assertEqual(base_data, proxy_proxy_data.replace('proxy', ''))
    418421
    419422
    420423def serializerTest(format, self):
Back to Top