标签:idm 用户 sts framework filter 就是 splay read_only cts
最后看出价的代码就是总代码
1. 查看竞价和出价是一个url
url(r‘^bid/$‘, bid.BidView.as_view()),
获取url传过来的值 是 重写 get_queryset
"""竞价""" from app01 import models from rest_framework.generics import ListAPIView, CreateAPIView from rest_framework import serializers class BidModelSerializer(serializers.ModelSerializer): status_text = serializers.CharField(source=‘get_status_display‘, read_only=True) username = serializers.CharField(source=‘user.nickname‘, read_only=True) class Meta: model = models.BidRecord fields = [‘id‘, ‘price‘, ‘item‘, ‘status_text‘, ‘username‘] # 竞价 GET: http://www.xxx.com/deposit/?item_id=1 # 提交竞价 POST: http://www.xxx.com/deposit/ class BidView(ListAPIView): queryset = models.BidRecord.objects.all().order_by(‘-id‘) serializer_class = BidModelSerializer def get_queryset(self): """ 获取传过来的值 :return: """ item_id = self.request.query_params.get(‘item_id‘) return self.queryset.filter(item_id=item_id) def get(self,request, *args, **kwargs): """ 页面两部分 上面的部分 加价,下面的部分是价格列表 :param request: :param args: :param kwargs: :return: """ response = super().get(request, *args, **kwargs) item_id = self.request.query_params.get(‘item_id‘) item_obj = models.AuctionItem.objects.filter(id=item_id).first() # 起拍价 item_obj.start_price # 出价记录最高价 # ① 倒序 取第一个 或者 # ② 当前单品的所有出价记录 models.BidRecord.objects.first(item_id=item_id) 在聚合一下 aggregate是取到一个值 # models.BidRecord.objects.first(item_id=item_id).aggregate(max_price=Max(‘price‘))是个字典 from django.db.models import Max max_price = models.BidRecord.objects.filter(item_id=item_id).aggregate(max_price=Max(‘price‘))[‘max_price‘] result = { ‘unit‘:item_obj.unit, ‘price‘:max_price or item_obj.start_price, ‘bit_list‘:response.data } response.data = result return response
2. 出价
"""竞价""" from app01 import models from rest_framework.generics import ListAPIView, CreateAPIView from rest_framework import serializers from rest_framework import exceptions from django.db import transaction from django.db.models import Max class BidModelSerializer(serializers.ModelSerializer): status_text = serializers.CharField(source=‘get_status_display‘, read_only=True) username = serializers.CharField(source=‘user.nickname‘, read_only=True) class Meta: model = models.BidRecord fields = [‘id‘, ‘price‘, ‘item‘, ‘status_text‘, ‘username‘] # 校验提交过来的数据 def validate_item(self, value): """ 一定是在拍卖中 """ exists = models.AuctionItem.objects.filter(id=value, status=3).exists() if not exists: raise exceptions.ValidationError(‘已拍卖完成或未开拍‘) return value def validate_price(self, value): # value=用户提交的价格 # 底价/加价幅度/最高价 item_id = self.initial_data.get(‘item‘) item_object = models.AuctionItem.objects.filter(id=item_id).first() if value <= item_object.start_price: raise exceptions.ValidationError(‘不能低于起拍价‘) div = (value - item_object.start_price) % item_object.unit if div: raise exceptions.ValidationError(‘必须按照加价幅度来竞价‘) max_price = models.BidRecord.objects.filter(item_id=item_id).aggregate(max_price=Max(‘price‘))[‘max_price‘] if not max_price: return value if max_price >= value: raise exceptions.ValidationError(‘已经有人出这个价了,你再涨涨‘) return value # 竞价 GET: http://www.xxx.com/deposit/?item_id=1 # 提交竞价 POST: http://www.xxx.com/deposit/ class BidView(ListAPIView, CreateAPIView): queryset = models.BidRecord.objects.all().order_by(‘-id‘) serializer_class = BidModelSerializer def get_queryset(self): """ 获取传过来的值 :return: """ item_id = self.request.query_params.get(‘item_id‘) return self.queryset.filter(item_id=item_id) def get(self,request, *args, **kwargs): """ 页面两部分 上面的部分 加价,下面的部分是价格列表 :param request: :param args: :param kwargs: :return: """ response = super().get(request, *args, **kwargs) item_id = self.request.query_params.get(‘item_id‘) item_obj = models.AuctionItem.objects.filter(id=item_id).first() # 起拍价 item_obj.start_price # 出价记录最高价 # ① 倒序 取第一个 或者 # ② 当前单品的所有出价记录 models.BidRecord.objects.first(item_id=item_id) 在聚合一下 aggregate是取到一个值 # models.BidRecord.objects.first(item_id=item_id).aggregate(max_price=Max(‘price‘))是个字典 max_price = models.BidRecord.objects.filter(item_id=item_id).aggregate(max_price=Max(‘price‘))[‘max_price‘] result = { ‘unit‘:item_obj.unit, ‘price‘:max_price or item_obj.start_price, ‘bit_list‘:response.data } response.data = result return response # 提交过来的出价 要保存 def perform_create(self, serializer): with transaction.atomic(): price = self.request.data.get(‘price‘) item_id = self.request.data.get(‘item‘) result = models.BidRecord.objects.filter(item_id=item_id).aggregate(max_price=Max(‘price‘)).select_for_update() max_price = result[‘max_price‘] if price > max_price: serializer.save(user=self.request.user) raise exceptions.ValidationError(‘已经被出价了,再涨涨.‘)
标签:idm 用户 sts framework filter 就是 splay read_only cts
原文地址:https://www.cnblogs.com/a438842265/p/12514517.html