drf图片字段序列化完整路径

Posted lyq-biu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了drf图片字段序列化完整路径相关的知识,希望对你有一定的参考价值。

一.需求

  前端需要它想要的数据格式:

    技术分享图片

  原有的数据格式:

技术分享图片

二.定制化:

  1.可以嵌套序列化pol_type,lit_des,area_detail,但结构如下:

class ChrDetailSerializer(serializers.ModelSerializer):
    """
    年代地层详情序列化
    """

    class Meta:
        model = Address_Lis
        fields = (region,)


class LisDetailSerializer(serializers.ModelSerializer):
    """
    岩石地层序列化
    """
    parent_categry = ChrDetailSerializer()

    class Meta:
        model = Address_Lis
        fields = (region, parent_categry)


class PolarizedTypeSeriliazer(serializers.ModelSerializer):
    """
    偏光类型序列化
    """

    class Meta:
        model = PolarizedType
        fields = (pol_type,)


class LithologicalSeriliazer(serializers.ModelSerializer):
    """
    岩性序列化
    """

    class Meta:
        model = Lithological
        fields = (lit_des,)

class RockDetailSerializer(serializers.ModelSerializer):
    """
    详情序列化
    """
    pol_type = PolarizedTypeSeriliazer()
    lit_des = LithologicalSeriliazer()
    area_detail = LisDetailSerializer()

    class Meta:
        model = Rock
        fields = __all__

 

    于是前端就不高兴了,不行这样的格式,前端说写起来有点麻烦,于是乎改呗。

技术分享图片

  2. 想了下,利用serializer.SerializerMethodField()好像还挺容易解决的:

    好像是解决了,一看我去,完了,这个图片的字段不是完整的url啊,一想,我不可能把域名放在setting中,然后拼接吧,就太low了。

class RockDetailSerializer(serializers.ModelSerializer):
    """
    详情序列化
    """

    detail = serializers.SerializerMethodField(read_only=True)
    area_detail = serializers.SerializerMethodField(read_only=True)
    pol_type = serializers.SerializerMethodField(read_only=True)
    lit_des = serializers.SerializerMethodField(read_only=True)
    well_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Rock
        fields = (detail, area_detail, pol_type, lit_des, depth, well_name)

    def get_detail(self, obj):
        try:
            return {id: obj.id, image: obj.image.url, lit_com: obj.lit_com, multiple: obj.multiple,
                    color: obj.color, pal_fea: obj.pal_fea, lit_fea: obj.lit_fea, por_fea: obj.por_fea}
        except:
            return None

    def get_area_detail(self, obj):
        try:
            add_obj = obj.area_detail
            return str(add_obj.parent_categry.region) + - + str(add_obj.region)
        except:
            return None

    def get_pol_type(self, obj):
        try:
            return obj.pol_type.pol_type
        except:
            return None

    def get_lit_des(self, obj):
        try:
            return obj.lit_des.lit_des
        except:
            return None

    def get_well_name(self, obj):
        try:
            return obj.area_detail.parent_categry.parent_categry.region
        except:
            return None

技术分享图片

  3.心想,可以在写一个序列化啊,于是又开干了:

    

class RockBasicSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rock
        fields = (id, image, lit_com, multiple, color, pal_fea, lit_fea, por_fea)


class RockDetailSerializer(serializers.ModelSerializer):
    """
    详情序列化
    """

    detail = serializers.SerializerMethodField(read_only=True)
    area_detail = serializers.SerializerMethodField(read_only=True)
    pol_type = serializers.SerializerMethodField(read_only=True)
    lit_des = serializers.SerializerMethodField(read_only=True)
    well_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Rock
        fields = (detail, area_detail, pol_type, lit_des, depth, well_name)

    def get_detail(self, obj):
        try:
             rock_obj = RockBasicSerializer(obj, many=False, context={request: self.context[request]}).data
             return rock_obj
        except:
            return None

    def get_area_detail(self, obj):
        try:
            add_obj = obj.area_detail
            return str(add_obj.parent_categry.region) + - + str(add_obj.region)
        except:
            return None

    def get_pol_type(self, obj):
        try:
            return obj.pol_type.pol_type
        except:
            return None

    def get_lit_des(self, obj):
        try:
            return obj.lit_des.lit_des
        except:
            return None

    def get_well_name(self, obj):
        try:
            return obj.area_detail.parent_categry.parent_categry.region
        except:
            return None

技术分享图片

哎哎哎,较好的解决了,夸自己一下。

 

    

 

 

 

    

   

以上是关于drf图片字段序列化完整路径的主要内容,如果未能解决你的问题,请参考以下文章

drf序列化高级自定义只读只写序列化覆盖字段二次封装Response数据库查询优化(断关联)十大接口视图家族自动补全图片链接

DRF框架serializer之ModelSerializer

DRF序列化代码

DRF框架序列化组件——字段验证

DRF - 具有直通模型的 m2m 字段的嵌套序列化

序列化 DRF 中的自定义相关字段