class PlatformData(View): def get(self, request): r = dict() r["errno"] = 1 r["order_total_num"] = OrderModel.objects.all().count() r["product_total_num"] = ProductModel.objects.all().count() r["success_order"] = OrderModel.objects.filter(state=2).count() r["cancel_order"] = OrderModel.objects.filter(state=4).count() r["nopay_order"] = OrderModel.objects.filter(state=1).count() r["trade_amount"] = serializer(RechargeModel.objects.all().aggregate(Sum("amount"))) return HttpResponse(json.dumps(r)) class DailyAmount(View): def get(self, request): daily_list = [] for i in range(0, 31): dtime = datetime.datetime.today() - datetime.timedelta(days=i) daily_dict = {} dailyAmount = serializer( RechargeModel.objects.filter(trade_no__month=dtime.month, trade_no__day=dtime.day).aggregate( damount=Sum('amount'))) amount = dailyAmount.pop('damount') timestamp = time.mktime(dtime.timetuple()) daily_dict[timestamp] = amount daily_list.append(daily_dict) return HttpResponse(json.dumps(daily_list)) class TayAmount(View): def get(self, request): today = datetime.datetime.today().day tomonth = datetime.datetime.today().month tAmount = serializer(RechargeModel.objects.filter(trade_no__month=tomonth, trade_no__day=today). aggregate(todayAmount=Sum('amount'))) yesterday = datetime.datetime.now() - datetime.timedelta(days=1) tomonth = yesterday.month today = yesterday.day yAmount = serializer( RechargeModel.objects.filter(trade_no__month=tomonth, trade_no__day=today).values( 'amount').aggregate(yesterdayAmount=Sum('amount'))) tAmount.update(yAmount) return HttpResponse(json.dumps(tAmount)) class DailyNum(View): def get(self, request): daily_list = [] r = dict() for i in range(0, 31): dtime = datetime.datetime.today() - datetime.timedelta(days=i) daily_dict = {} dailyNum = serializer( OrderModel.objects.filter(date_created__month=dtime.month, date_created__day=dtime.day).count()) timestamp = time.mktime(dtime.timetuple()) daily_dict[int(timestamp)] = dailyNum daily_list.append(daily_dict) r["errno"] = 1 r["daily_list"] = daily_list return HttpResponse(json.dumps(r)) class TayNum(View): def get(self, request): today = datetime.datetime.today().day tomonth = datetime.datetime.today().month tCount = serializer(OrderModel.objects.filter(date_created__month=tomonth, date_created__day=today).count()) yesterday = datetime.datetime.now() - datetime.timedelta(days=1) tomonth = yesterday.month today = yesterday.day yCount = serializer( OrderModel.objects.filter(date_created__month=tomonth, date_created__day=today).count()) r = dict() r['errno'] = 1 r['todayNum'] = tCount r['yesterdayNum'] = yCount return HttpResponse(json.dumps(r))
models
date_created = models.DateTimeField(default=timezone.now, verbose_name='订单创建时间')
views
orderAmount = serializer( OrderModel.objects.values(time=TruncDay('date_created')).annotate(num=Count('id'))) (查出来是字典的格式,time是key对应时间戳,num是key对应的是数量)