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

(二十六)拍卖专场相关的接口

时间:2020-03-17 19:49:17      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:api   play   ext   min   rgs   format   war   price   nbsp   

1. 拍卖专场的接口

url(r^auction/$, auction.AuctionView.as_view()),

class AuctionModelView(serializers.ModelSerializer): status = serializers.CharField(source=get_status_display) preview_start_time = serializers.DateTimeField(format="%Y-%m-%d") goods = serializers.SerializerMethodField() class Meta: model = models.Auction # fields = "__all__" fields = [id, title, cover, status, preview_start_time, look_count, goods_count, total_price, bid_count, goods] def get_goods(self, obj): queryset = models.AuctionItem.objects.filter(auction=obj)[0:5] return [row.cover for row in queryset] class AuctionView(ListAPIView): """ 拍卖专场接口 """ # queryset = models.Auction.objects.filter(status__gt=1).order_by(‘id‘) queryset = models.Auction.objects.filter(status__gt=0).order_by(id) serializer_class = AuctionModelView # 分页接口 # filter_backends = [MinFilterBackend, MaxFilterBackend] # pagination_class = OldBoyLimitPagination

2. 拍卖专场详细

先获取专场详细---在里面获取商品信息在写一个序列化循每一个商品的信息
url(r^auction/(?P<pk>\d+)/$, auction.AuctionDetailView.as_view()),

class AuctionDetailItemModelSerializer(serializers.ModelSerializer): is_deposit = serializers.SerializerMethodField() class Meta: model = models.AuctionItem fields = [id, cover, status, reserve_price, highest_price, is_deposit] def get_is_deposit(self, obj): user_object = self.context[request].user if not user_object: return False return models.DepositRecord.objects.filter(user=user_object, item=obj, status=2, deposit_type=1).exists() class AuctionModelDetailView(serializers.ModelSerializer): goods = serializers.SerializerMethodField() # 检查是否缴纳保证金 is_deposit = serializers.SerializerMethodField() model = models.Auction fields = __all__ def get_goods(self, obj): item_object_list = models.AuctionItem.objects.filter(auction=obj)
      # 每个单品的信息 ser
= AuctionDetailItemModelSerializer(instance=item_object_list, many=True, context=self.context) return ser.data def get_is_deposit(self, obj): """ 检查是否已缴纳全场保证金 """ # 1. 没登陆,显示去缴纳保证金 user_object = self.context[request].user if not user_object: return False # 2. 去查看缴纳保证金记录的表中是否有此用户&此专场 return models.DepositRecord.objects.filter(user=user_object, auction=obj, status=2, item__isnull=True).exists() class AuctionDetailView(RetrieveAPIView): """ 拍卖列表详细页面 专场详细 """ queryset = models.Auction.objects.filter(status__gt=1) serializer_class = AuctionModelDetailView

3. 整合 拍卖专场和拍卖详细

    # ‘‘‘继承GenericViewSet要写get对应的是啥‘‘‘
    url(r^auction2/$, auction.Auction2View.as_view({get:list})),
    url(r^auction2/(?P<pk>\d+)/$, auction.Auction2View.as_view({get:retrieve})),



class Auction2View(ListModelMixin, RetrieveModelMixin, GenericViewSet):
    queryset = models.Auction.objects.filter(status__gt=1).order_by(-id)
    serializer_class = AuctionModelSerializerView
    filter_backends = [MinFilterBackend, MaxFilterBackend, ]
    pagination_class = OldBoyLimitPagination

    def get_serializer_class(self):
        pk = self.kwargs.get(pk)
        if pk:
            return AuctionModelDetailView
        return AuctionModelSerializerView

↓ 对应的序列化
# ##############拍卖专场接口###################################
class AuctionModelSerializerView(serializers.ModelSerializer):
    status = serializers.CharField(source=get_status_display)
    preview_start_time = serializers.DateTimeField(format="%Y-%m-%d")

    goods = serializers.SerializerMethodField()

    class Meta:
        model = models.Auction
        # fields = "__all__"
        fields = [id, title, cover, status, preview_start_time,
                  look_count, goods_count, total_price, bid_count, goods]

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

# #######拍卖列表详细页面 专场详细##########################################

class AuctionDetailItemModelSerializer(serializers.ModelSerializer):
    is_deposit = serializers.SerializerMethodField()

    class Meta:
        model = models.AuctionItem
        fields = [id, cover, status, reserve_price, highest_price, is_deposit]

    def get_is_deposit(self, obj):
        user_object = self.context[request].user
        if not user_object:
            return False
        return models.DepositRecord.objects.filter(user=user_object, item=obj, status=2, deposit_type=1).exists()


class AuctionModelDetailView(serializers.ModelSerializer):
    goods = serializers.SerializerMethodField()
    # 检查是否缴纳保证金
    is_deposit = serializers.SerializerMethodField()

    model = models.Auction
    fields = __all__

    def get_goods(self, obj):
        # 每个单品信息
        item_object_list = models.AuctionItem.objects.filter(auction=obj)
        ser = AuctionDetailItemModelSerializer(instance=item_object_list, many=True, context=self.context)
        return ser.data

    def get_is_deposit(self, obj):
        """ 检查是否已缴纳全场保证金 """
        # 1. 没登陆,显示去缴纳保证金
        user_object = self.context[request].user
        if not user_object:
            return False
        # 2. 去查看缴纳保证金记录的表中是否有此用户&此专场
        return models.DepositRecord.objects.filter(user=user_object, auction=obj, status=2, item__isnull=True).exists()

 

(二十六)拍卖专场相关的接口

标签:api   play   ext   min   rgs   format   war   price   nbsp   

原文地址:https://www.cnblogs.com/a438842265/p/12512694.html

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