码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-29 18:33:55      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:ext   nbsp   ali   需要   method   lock   ola   tin   text   

一.需求

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

    技术分享图片

  原有的数据格式:

技术分享图片

二.定制化:

  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图片字段序列化完整路径

标签:ext   nbsp   ali   需要   method   lock   ola   tin   text   

原文地址:https://www.cnblogs.com/lyq-biu/p/10334247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!