Django serializer 中 将相对网址替换为绝对网址

例子

class GoodsSerializer(serializers.ModelSerializer):
    goods_desc = serializers.SerializerMethodField(help_text='内容', label='内容')

    def get_goods_desc(self, instance: Goods):
        goods_desc = instance.goods_desc.replace('<img src="' + MEDIA_URL,
                                                 '<img src="' + self.context['request'].build_absolute_uri(
                                                     MEDIA_URL))
        return goods_desc

    class Meta:
        model = Goods
        fields = '__all__'

猜你喜欢

转载自www.cnblogs.com/edhg/p/10858648.html
0条评论
添加一条新回复