首页 > 其他分享 >DRF中后台返回不同状态,前端展示不同状态样式

DRF中后台返回不同状态,前端展示不同状态样式

时间:2022-11-07 03:33:06浏览次数:71  
标签:status 状态 obj get 不同 items serializers class DRF

后端

class AuctionModelSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    items = serializers.SerializerMethodField()
    cover = serializers.CharField()

    class Meta:
        model = models.Auction
        fields = ['id', 'title', 'status', 'cover', 'total_price', 'look_count', 'goods_count', 'items']

    def get_status(self, obj):
        status_class_mapping = {
            2: 'preview',
            3: 'auction',
            4: 'stop'
        }
        return {'text': obj.get_status_display(), 'class': status_class_mapping.get(obj.status)}

    def get_items(self, obj):
        queryset = models.AuctionItem.objects.filter(auction=obj)[0:5]
        return [row.cover.name for row in queryset]

前端

<view>
  <text class="status {{auctionDetail.status.class}}">{{auctionDetail.status.text}}</text>
  <text class="time">距离开始 34时 42分 33秒</text>
</view>

img

标签:status,状态,obj,get,不同,items,serializers,class,DRF
From: https://www.cnblogs.com/fuminer/p/16864774.html

相关文章