Serialize Model from Derived (Abstract) Model

寵の児 提交于 2021-01-28 19:43:17

问题


I have the following models:

class Address(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    ....
    class Meta:
        abstract = True

class BillingAddress(Address):
    is_default = models.BooleanField()
    ...
    class Meta:
        db_table = 'billing_address'

I'm trying to build a serializer for BillingAddress:

class AddressSerializer(serializers.ModelSerializer):
    class Meta:
        abstract = True
        model = AddressModel

class BillingAddressSerializer(AddressSerializer):
    def to_representation(self, obj):
        return AddressSerializer(obj, context=self.context).to_representation(obj)

    class Meta(AddressSerializer.Meta):
        model = UserBillingAddress
        fields = (
            'id',
            'is_default',
        )

I keep getting:

 ValueError: Cannot use ModelSerializer with Abstract Models.

How can I build my BillingAddressSerializer to reflect both classes?

来源:https://stackoverflow.com/questions/61718995/serialize-model-from-derived-abstract-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!