python相关

Django REST框架中对Serializer嵌套资源进行自

2019-01-03  本文已影响0人  十万个榴莲饼

前段时间在项目中实现假删除功能,发现一个问题:以‘专家’和‘专家组’为例,‘专家’外键关联‘专家组’,对‘专家组’进行假删后,‘专家’的serializer中的嵌套显示数据依然会显示被假删的 组。
这个问题的解决办法就是需要对嵌套资源进行自定义过滤(本例就是做了假删除的组过滤掉,即去掉isDelete=True的‘专家组’数据)。
model如下:

class ExpertGroup(models.Model):
    """
    专家组列表
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name=u'编号', help_text=u'编号')
    groupName = models.CharField(max_length=100, db_column="groupname", verbose_name=u"专家组名", help_text='专家组名',
                                 null=True, blank=True)
    isDelete = models.BooleanField(db_column="isdelete", verbose_name=u'是否已删除', help_text="是否已删除", default=False)

    class Meta:
        db_table = 'sys_expertgroup'
        verbose_name = verbose_name_plural = "专家组列表"

    def __str__(self):
        return str(self.groupName)

class Expert(models.Model):
    """
    专家列表
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name=u'编号', help_text=u'编号')
    expert = models.ForeignKey(User, db_column="expert", verbose_name=u"专家", help_text='专家',
                               on_delete=models.SET_NULL, related_name="expert_user", null=True, blank=True)
    expertGroup = models.ForeignKey(ExpertGroup, db_column="expertgroup", verbose_name=u"专家组", help_text='专家组',
                               on_delete=models.SET_NULL, related_name="expert_expertgroup", null=True, blank=True)
    
    isDelete = models.BooleanField(db_column="isdelete", verbose_name=u'是否删除', help_text="是否删除", default=False)
   
    class Meta:
        db_table = 'sys_expert'
        verbose_name = verbose_name_plural = "专家列表"

    def __str__(self):
        return str(self.id)

要将假删除做到像真删除那样,需要两步:
1.对‘专家’的filter进行修改,不再显示已假删除的字段:
原代码:

class ExpertFilter(filters.FilterSet):
 
    class Meta:
        model = Expert
        fields = ["id", "expertGroup"]

修改后:

class ExpertFilter(filters.FilterSet):
    expertgroups = ExpertGroup.objects.filter(isDelete=False).values_list('id', 'groupName')
    expertGroup = filters.ChoiceFilter(choices=expertgroups, label=u'专家组', help_text="专家组")

    class Meta:
        model = Expert
        fields = ["id", "expertGroup"]

效果就是下图中的‘专家’的过滤器中,外键关联的‘专家组’字段中不再显示已假删除的组了。


image.png

2.对‘专家’的Serializer进行修改(重要!核心在此,重写to_representation方法),嵌套数据中过滤掉已假删除的数据:
原代码:

class ExpertSerializer(serializers.ModelSerializer):
    expertGroup = ExpertGroupListSerializer(many=False)

    class Meta:
        model = Expert
        fields = '__all__'

修改后:

class ExpertSerializer(serializers.ModelSerializer):
    expertGroup = ExpertGroupListSerializer(many=False)

    def to_representation(self, obj):
        data = super(ExpertSerializer, self).to_representation(obj)
        data['expertGroup'] = ExpertGroupListSerializer(
            ExpertGroup.objects.filter(isDelete=False, expert_expertgroup=obj).first()).data
        return data

    class Meta:
        model = Expert
        fields = '__all__'

修改后serializer效果如下图:


image.png

已被假删除的‘专家组’显示为空。
总结:
除了假删除,还可以过滤掉其他不想要显示的字段,核心就是重写to_representation方法,对嵌套数据进行过滤。
-----完

上一篇下一篇

猜你喜欢

热点阅读