1.

复杂版

统计,通过跨表查询和timedate模块过滤找到

 1 from django.db.models import Count
 2 
 3 class TongJiView(View):
 4     def today(self):
 5         import datetime
 6         today=datetime.datetime.now().date()
 7         customer_list=Customer.objects.filter(deal_date=today)
 8 
 9         # 查询每一个销售的名字以及今天对应的成单量
10         ret=UserInfo.objects.filter(depart_id=2,customers__deal_date=today).annotate(c=Count("customers")).values_list("username","c")
11         print(ret)
12         ret=[[item[0],item[1]] for item in list(ret)]
13 
14         return {"customer_list":customer_list,"ret":list(ret)}
15 
16     def zuotian(self):
17         import datetime
18         zuotian = datetime.datetime.now().date()-datetime.timedelta(days=1)
19         customer_list = Customer.objects.filter(deal_date=zuotian)
20 
21         # 查询每一个销售的名字以及昨天对应的成单量
22         ret = UserInfo.objects.filter(depart_id=2, customers__deal_date=zuotian).annotate(
23             c=Count("customers")).values_list("username", "c")
24         print(ret)
25         print(ret)
26         ret = [[item[0], item[1]] for item in list(ret)]
27 
28         return {"customer_list": customer_list, "ret": list(ret)}
29 
30 
31 
32     def week(self):
33         import datetime
34         today = datetime.datetime.now().date()
35         weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=1)
36         customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today)
37 
38         # 查询每一个销售的名字以及昨天对应的成单量
39         ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate(
40             c=Count("customers")).values_list("username", "c")
41         print(ret)
42 
43         print(ret)
44         ret = [[item[0], item[1]] for item in list(ret)]
45 
46         return {"customer_list": customer_list, "ret": list(ret)}
47 
48 
49     def recent_month(self):
50         import datetime
51         today = datetime.datetime.now().date()
52         weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=4)
53         customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today)
54 
55         # 查询每一个销售的名字以及昨天对应的成单量
56         ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate(
57             c=Count("customers")).values_list("username", "c")
58         print(ret)
59 
60         print(ret)
61         ret = [[item[0], item[1]] for item in list(ret)]
62 
63         return {"customer_list": customer_list, "ret": list(ret)}
64 
65     def get(self,request):
66 
67         date=request.GET.get("date","today")
68 
69         if hasattr(self,date):
70             context=getattr(self,date)()
71 
72         return render(request,"customer/tongji.html",context)

后端.py