[django]django models最佳实战
models
class People(models.Model):
name = models.CharField(max_length=30)
age = models.CharField(max_length=30)
def __str__(self):
return self.name
insert into app03_people (name,age) values('m1','1');
查出最小的age
使用annotate(Min('age')出错
In [1]: from django.db.models import Max,Min,Sum,Avg,Count
In [2]: from app03.models import People
In [3]: People.objects.all()
Out[3]: <QuerySet [<People: m1>, <People: m2>, <People: m3>, <People: m4>]>
In [6]: People.objects.annotate(Min('age'))
Out[6]: <QuerySet [<People: m1>, <People: m2>, <People: m3>, <People: m4>]>
In [10]: print(People.objects.annotate(Min('age')).query)
SELECT `app03_people`.`id`, `app03_people`.`name`, `app03_people`.`age`, MIN(`app03_people`.`age`) AS `age__min` FROM `app03_people` GROUP BY `app03_people`.`id` ORDER BY NULL
小结: People.objects.annotate(Min('age'))默认group by id;
使用aggregate完美解决
In [13]: print(People.objects.aggregate(Min('age'))) # 主这里无法query, 因为他返回的是字典类型.
{'age__min': '1'}
等同的sql,(聚合函数可以单独使用)
mysql> SELECT MIN(`app03_people`.`age`) AS `age__min` FROM `app03_people`;
+----------+
| age__min |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
查询age最大最小
aggregate解决
In [15]: People.objects.aggregate(Min('age'),Max('age'))
Out[15]: {'age__min': '1', 'age__max': '4'}
等价的sql
mysql> SELECT MIN(`app03_people`.`age`) AS `age__min`,MAX(`app03_people`.`age`) AS `age__max` FROM `app03_people`;
+----------+----------+
| age__min | age__max |
+----------+----------+
| 1 | 4 |
+----------+----------+
1 row in set (0.00 sec)
annotate分组查询
In [6]: People.objects.values('part').annotate(Min('age'))
Out[6]: <QuerySet [{'part': 'UI', 'age__min': '10'}, {'part': 'python', 'age__min': '30'}, {'part': 'java', 'age__min': '40'}]>
In [7]: print(People.objects.values('part').annotate(Min('age')).query)
SELECT `app03_people`.`part`, MIN(`app03_people`.`age`) AS `age__min` FROM `app03_people` GROUP BY `app03_people`.`part` ORDER BY NULL
默认值显示两个字段(正常)
mysql> SELECT `app03_people`.`part`, MIN(`app03_people`.`age`) AS `age__min` FROM `app03_people` GROUP BY `app03_people`.`part` ORDER BY NULL
-> ;
+--------+----------+
| part | age__min |
+--------+----------+
| UI | 10 |
| python | 30 |
| java | 40 |
+--------+----------+
3 rows in set (0.00 sec)
In [17]: print(People.objects.values('part').annotate(Min('age'),Max('age')).query);
SELECT `app03_people`.`part`, MIN(`app03_people`.`age`) AS `age__min`, MAX(`app03_people`.`age`) AS `age__max` FROM `app03_people` GROUP BY `app03_people`.`part` ORDER BY NULL
mysql> SELECT `app03_people`.`part`, MIN(`app03_people`.`age`) AS `age__min`, MAX(`app03_people`.`age`) AS `age__max` FROM `app03_people` GROUP BY `app03_people`.`part` ORDER BY NULL;
+--------+----------+----------+
| part | age__min | age__max |
+--------+----------+----------+
| UI | 10 | 20 |
| python | 30 | 50 |
| java | 40 | 40 |
+--------+----------+----------+
3 rows in set (0.00 sec)
In [19]: print(People.objects.values('part').annotate(Min('age')).annotate(Max('age')).query);
SELECT `app03_people`.`part`, MIN(`app03_people`.`age`) AS `age__min`, MAX(`app03_people`.`age`) AS `age__max` FROM `app03_people` GROUP BY `app03_people`.`part` ORDER BY NULL
mysql> SELECT `app03_people`.`part`, MIN(`app03_people`.`age`) AS `age__min`, MAX(`app03_people`.`age`) AS `age__max` FROM `app03_people` GROUP BY `app03_people`.`part` ORDER BY NULL;
+--------+----------+----------+
| part | age__min | age__max |
+--------+----------+----------+
| UI | 10 | 20 |
| python | 30 | 50 |
| java | 40 | 40 |
+--------+----------+----------+
3 rows in set (0.00 sec)
以2个字段为准 分类
model
In [157]: print ChainLog.objects.values('src_svc_id', 'dst_svc_id').annotate(
...: is_success_total_count=Count('is_success')).query
SELECT `home_application_chainlog`.`src_svc_id`, `home_application_chainlog`.`dst_svc_id`, COUNT(`home_application_chainlog`.`is_success`) AS `is_success_total_count` FROM `home_application_chainlog` GROUP BY `home_application_chainlog`.`src_svc_id`, `home_application_chainlog`.`dst_svc_id` ORDER BY NULL
mysql> SELECT `home_application_chainlog`.`src_svc_id`, `home_application_chainlog`.`dst_svc_id`, COUNT(`home_application_chainlog`.`is_success`) AS `is_success_total_count` FROM `home_application_chainlog` GROUP BY `home_application_chainlog`.`src_svc_id`, `home_application_chainlog`.`dst_svc_id` ORDER BY NULL;
+------------+------------+------------------------+
| src_svc_id | dst_svc_id | is_success_total_count |
+------------+------------+------------------------+
| 1 | 1 | 2 |
| 1 | 2 | 1 |
| 2 | 3 | 1 |
| 2 | 4 | 1 |
| 3 | 5 | 1 |
+------------+------------+------------------------+
5 rows in set (0.00 sec)
如果是py字典,以2个字段为维度,不好统计
total = [
{'from':1,'to':2,'is_success':1},
{'from':1,'to':3,'is_success':0},
{'from':1,'to':4,'is_success':1}
]
faild = [
{'from':1,'to':2,'is_success':1},
{'from':1,'to':3,'is_success':0},
{'from':1,'to':4,'is_success':1}
]
[django]django models最佳实战的更多相关文章
- [django]django查询最佳实战
from django.db.models import Max, Min, Sum, Avg, Count, Q, F Django中的F和Q函数 一.F介绍 作用:操作数据表中的某列值,F()允许 ...
- Django 二——models(admin、ORM),一对一、一对多、多对多操作,all、values、value_list的对比
内容概要 1.关系对象映射ORM 2.admin的配置(选修) 3.all().values().value_list()的对比 4.数据库操作(一对一.一对多.多对多) 5.HttpResponse ...
- web框架-(七)Django补充---models进阶操作及modelform操作
通过之前的课程我们可以对于Django的models进行简单的操作,今天了解下进阶操作和modelform: 1. Models进阶操作 1.1 字段操作 AutoField(Field) - int ...
- 【Django】--Models 和ORM以及admin配置
Models 数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库 <1>sqlite django默认使用sqlite的数据库 ...
- Django 之 models的 F() 和 Q() 函数
前提: app名称为core,models.py 如下: #coding: utf8 import datetime from django.db import models class Order( ...
- 【Python】django模型models的外键关联使用
Python 2.7.10,django 1.8.6 外键关联:http://www.bubuko.com/infodetail-618303.html 字段属性:http://www.cnblogs ...
- django - from django.db.models import F - class F
F() 的执行不经过 python解释器,不经过本机内存,是生成 SQL语句的执行. # Tintin filed a news story! reporter = Reporters.objects ...
- Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt)
Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt) 作者: Desmond Chen,发布日期: 2014-05- ...
- Django的Models(三)
ORM操作的一些进阶的方法: ################################################################## # PUBLIC METHODS T ...
随机推荐
- python搭建简单http文件服务器
import SimpleHTTPServer import SocketServer PORT = 8000 Handler = SimpleHTTPServer.SimpleHTTPRequest ...
- 评估分类器性能的度量,像混淆矩阵、ROC、AUC等
评估分类器性能的度量,像混淆矩阵.ROC.AUC等 内容概要¶ 模型评估的目的及一般评估流程 分类准确率的用处及其限制 混淆矩阵(confusion matrix)是如何表示一个分类器的性能 混淆矩阵 ...
- 初学的linux命令行
这几条命令是今天刚初学的,以前总看别人输入命令,好利落,到自己了,真心觉得难.目前就学了这几个命令.后期等学会了,再进行添加 vm 文件名 ——> 新建文件: :wq ——> 保存并退 ...
- Dapper的数据库连接管理(打开、关闭)
Dapper对于数据库连接的管理:如果已经打开,它会关闭连接.如果你只是做一个快速查询-让Dopter自己处理它. 如果你做了很多事情,你应该自己打开连接,并在最后关闭连接,所有的查询在中…只是从效率 ...
- [No0000BC]ADO.NET中的几个主要对象
ADO 指 ActiveX 数据对象(ActiveX Data Objects). 从一个 ASP 页面内部访问数据库的通常的方法是: 创建一个到数据库的 ADO 连接 打开数据库连接 创建 ADO ...
- ==、===和Object.is()的区别
==.===和Object.is()的区别 一. 定义: ==:等同,比较运算符,两边值类型不同的时候,先进行类型转换,再比较: ===:恒等,严格比较运算符,不做类型转换,类型不同就是不等: Obj ...
- 用NFS挂载root出现:NFS: failed to create MNT RPC client, status=-101(-110)
2014-02-18 08:06:17 By Ly #Linux 阅读(78) 评论(0) 错误信息如下: Root-NFS: nfsroot=/home/zenki/nfs/rootfs NFS ...
- PHP之伪类型与变量
本文档中使用的伪类型与变量 伪类型(pseudo -types)是PHP文档里用于指示参数可以使用的类型和值, 请注意,它们不是PHP语言里原生类型, 所以不能把伪类型用于自定义函数里面的类型约束(t ...
- 2016年蓝桥杯省赛A组c++第8题(暴力求解)
/* 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和. 如果把0包括进去,就正好可以表示为4个数的平方和. 比如: 5 = 0^2 + 0^2 + 1^2 + 2^2 ...
- DOM节点的基础操作
1.寻找节点 //寻找节点 id方法 document.getElementById(); //标准 //寻找节点层次方法 parentNode().firstChild()和lastChild(): ...