django 统计表
1.
复杂版
统计,通过跨表查询和timedate模块过滤找到
from django.db.models import Count class TongJiView(View):
def today(self):
import datetime
today=datetime.datetime.now().date()
customer_list=Customer.objects.filter(deal_date=today) # 查询每一个销售的名字以及今天对应的成单量
ret=UserInfo.objects.filter(depart_id=2,customers__deal_date=today).annotate(c=Count("customers")).values_list("username","c")
print(ret)
ret=[[item[0],item[1]] for item in list(ret)] return {"customer_list":customer_list,"ret":list(ret)} def zuotian(self):
import datetime
zuotian = datetime.datetime.now().date()-datetime.timedelta(days=1)
customer_list = Customer.objects.filter(deal_date=zuotian) # 查询每一个销售的名字以及昨天对应的成单量
ret = UserInfo.objects.filter(depart_id=2, customers__deal_date=zuotian).annotate(
c=Count("customers")).values_list("username", "c")
print(ret)
print(ret)
ret = [[item[0], item[1]] for item in list(ret)] return {"customer_list": customer_list, "ret": list(ret)} def week(self):
import datetime
today = datetime.datetime.now().date()
weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=1)
customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today) # 查询每一个销售的名字以及昨天对应的成单量
ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate(
c=Count("customers")).values_list("username", "c")
print(ret) print(ret)
ret = [[item[0], item[1]] for item in list(ret)] return {"customer_list": customer_list, "ret": list(ret)} def recent_month(self):
import datetime
today = datetime.datetime.now().date()
weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=4)
customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today) # 查询每一个销售的名字以及昨天对应的成单量
ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate(
c=Count("customers")).values_list("username", "c")
print(ret) print(ret)
ret = [[item[0], item[1]] for item in list(ret)] return {"customer_list": customer_list, "ret": list(ret)} def get(self,request): date=request.GET.get("date","today") if hasattr(self,date):
context=getattr(self,date)() return render(request,"customer/tongji.html",context)
后端.py
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> </head>
<body>
<h3>客户成单量统计</h3> <hr>
<a href="?date=today">今天</a>
<a href="?date=zuotian">昨天</a>
<a href="?date=week">最近一周</a>
<a href="?date=recent_month">最近一个月</a>
<hr> <div class="container">
<div class="row">
<div class="col-md-12">
<table id="example2" class="text-center table table-bordered table-hover">
<thead>
<tr>
<th class="text-center">编号</th>
<th class="text-center">客户姓名</th>
<th class="text-center">性别</th>
<th class="text-center">客户来源</th>
<th class="text-center">销售</th>
<th class="text-center">所报班级</th>
</tr>
</thead>
<tbody> {% for customer in customer_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ customer.name }}</td>
<td>{{ customer.get_sex_display }}</td>
<td>{{ customer.get_source_display }}</td>
<td>{{ customer.consultant }}</td>
<td>{{ customer.get_classlist }}</td> </tr>
{% endfor %} </tbody>
</table>
<hr>
<div id="container" style="width:600px;height:400px"></div>
</div>
</div>
</div> <script src="/static/highchart/highcharts.js"></script>
<script>
var chart = Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: '统计成单量'
},
subtitle: {
text: '数据截止 2017-03,来源: <a href="https://en.wikipedia.org/wiki/List_of_cities_proper_by_population">Wikipedia</a>'
},
xAxis: {
type: 'category',
labels: {
rotation: 0 // 设置轴标签旋转角度
}
},
yAxis: {
min: 0,
title: {
text: '成单数'
}
},
legend: {
enabled: false
},
tooltip: {
pointFormat: '成单人数: <b>{point.y} 单</b>'
},
series: [{
name: '总人口',
data: {{ ret|safe }},
dataLabels: {
enabled: true,
rotation: -90,
color: '#FFFFFF',
align: 'right',
format: '{point.y:.1f}', // :.1f 为保留 1 位小数
y: 10
}
}]
}); </script>
</body>
</html>
前端
2.简单版
from django.views import View
from app01.models import Customer,UserInfo
from django.db.models import Count class TongJiView2(View): def get(self,request):
date=request.GET.get("date","today")
# func=getattr(self,date)
#ret=func()
import datetime
now=datetime.datetime.now().date()
delta1=datetime.timedelta(days=1)
delta2=datetime.timedelta(weeks=1)
delta3=datetime.timedelta(weeks=4) condition={
"today":[{"deal_date__date":now},{"customers__deal_date":now}],
"yesterday":[{"deal_date__date":now-delta1},{"customers__deal_date":now-delta1}],
"week":[{"deal_date__gte":now-delta2,"deal_date__lte":now},
{"customers__deal_date__gte":now-delta2,"customers__deal_date__lte":now}
],
"recent_month":[{"deal_date__gte":now-delta3,"deal_date__lte":now},
{"customers__deal_date__gte":now-delta3,"customers__deal_date__lte":now}
],
} customer_list=Customer.objects.filter(**(condition.get(date)[0]))
ret=UserInfo.objects.all().filter(**(condition.get(date)[1])).annotate(c=Count("customers")).values_list("username","c")
ret = [[item[0], item[1]] for item in list(ret)] return render(request,"customer/tongji.html",locals())
后端.py
django 统计表的更多相关文章
- django从零开始-模型
1.设置统计表 配置models.py from django.db import models # Create your models here. # 发布会 class Event(models ...
- Django模型层之单表操作
Django模型层之单表操作 一 .ORM简介 我们在使用Django框架开发web应用的过程中,不可避免地会涉及到数据的管理操作(如增.删.改.查),而一旦谈到数据的管理操作,就需要用到数据库管理软 ...
- Django之模型层:表操作
目录 Django之模型层:表操作 一.ORM简介 django测试环境搭建 Django终端打印SQL语句 二 单表操作 2.1 按步骤创建表 2.2记录 三.多表操作 1 创建模型 2 添加.删除 ...
- Django之模型层第一篇:单表操作
Django之模型层第一篇:单表操作 一 ORM简介 我们在使用Django框架开发web应用的过程中,不可避免地会涉及到数据的管理操作(如增.删.改.查),而一旦谈到数据的管理操作,就需要用到数 ...
- 6、Django之模型层第一篇:单表操作
一 ORM简介 我们在使用Django框架开发web应用的过程中,不可避免地会涉及到数据的管理操作(如增.删.改.查),而一旦谈到数据的管理操作,就需要用到数据库管理软件,例如mysql.oracle ...
- 异步任务队列Celery在Django中的使用
前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- django server之间通过remote user 相互调用
首先,场景是这样的:存在两个django web应用,并且两个应用存在一定的联系.某些情况下彼此需要获取对方的数据. 但是我们的应用肯经都会有对应的鉴权机制.不会让人家随随便便就访问的对吧.好比上车要 ...
- Mysql事务探索及其在Django中的实践(二)
继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...
随机推荐
- 【BZOJ3529】[SDOI2014] 数表(莫比乌斯反演)
点此看题面 大致题意: 规定一个\(n*m\)数表中每个数为\(\sum_{d|i,d|j}d\),求数表中不大于\(a\)的数之和. 不考虑限制 我们先不考虑限制,来推一波式子. 首先,易知数表中第 ...
- [LeetCode] 924. Minimize Malware Spread 最大程度上减少恶意软件的传播
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...
- 简单模仿QQ聊天界面
首先看一下最终的效果,显示了消息时间,用户昵称,用户头像. 大致实现方法: 用最简单的ListView显示消息内容. 不同的用户使用不同的消息布局文件,从而达到头像左右显示的效果,如上图有2个用户&q ...
- Codeforces Round #606 (Div. 1) Solution
从这里开始 比赛目录 我菜爆了. Problem A As Simple as One and Two 我会 AC 自动机上 dp. one 和 two 删掉中间的字符,twone 删掉中间的 o. ...
- Visual Studio 调试系列3 断点
系列目录 [已更新最新开发文章,点击查看详细] 断点是开发人员的工具箱中最重要的调试技术之一. 若要暂停调试程序执行所需的位置设置断点. 例如,你可能想要查看代码变量的状态或查看调用堆栈的某些 ...
- FontForge 汉化教程
引用 :http://www.sucaijishi.com/2018/articles_0815/258.html FontForge是一款免费字库编辑工具,官方暂不提供简体中文,本文汉化方法在201 ...
- 容斥原理--计算错排的方案数 UVA 10497
错排问题是一种特殊的排列问题. 模型:把n个元素依次标上1,2,3.......n,求每一个元素都不在自己位置的排列数. 运用容斥原理,我们有两种解决方法: 1. 总的排列方法有A(n,n),即n!, ...
- Docker私有云管理平台————Docker Shipyard
一.shipyard中文版安装(CentOS) 注:本文安装操作均在root用户下,安装前需先安装Docker (传送门) 下载所需docker镜像 docker pull rethinkdb doc ...
- RocketMQ 使用情况梳理
个人梳理有限:欢迎大家 丰富此文档 2018年 12 月 RocketMQ 版本 不适用于 新版关系请勿参考目前规划原则: topic 创建基于业务 消费者基于模块 多对多关系 ...
- Skywalking入门介绍,skywalking6.5.0 +mysql (windows) 搭建
一. 介绍 1. 基本信息 SkyWalking 创建于2015年,提供分布式追踪功能.从5.x开始,项目进化为一个完成功能的Application Performance Monitoring系统. ...