django ORM练习

1. 分组
ORM中values或者values_list 里面写什么字段,就相当于select 什么字段

ret = models.Employee.objects.all().values("dept", "age")
相当于:
SELECT `employee`.`dept`, `employee`.`age` FROM `employee` LIMIT 21; args=()

 


2. ORM中 annotate 前面是什么就按照什么分组!
from django.db.models import Avg
ret = models.Employee.objects.values("province").annotate(a=Avg("salary")).values("province", "a")
相当于:
SELECT `employee`.`province`, AVG(`employee`.`salary`) AS `a` FROM `employee` GROUP BY `employee`.`province` ORDER BY NULL LIMIT 21; args=()

 

3. extra --> 在执行ORM查询的时候执行额外的SQL语句
# 查询person表,判断每个人的工资是否大于2000
ret = models.Person.objects.all().extra(
select={"gt": "salary > 2000"}
)
相当于:
SELECT (salary > 2000) AS `gt`, `person`.`id`, `person`.`name`, `person`.`salary`, `person`.`dept_id` FROM `person` LIMIT 21; args=()

 


4. 直接执行原生的SQL语句,类似pymysql的用法
from django.db import connection
cursor = connection.cursor() # 获取光标,等待执行SQL语句
cursor.execute("""SELECT * from person where id = %s""", [1])
row = cursor.fetchone()
print(row)

posted @ 2018-07-26 17:34  篓子  阅读(155)  评论(0编辑  收藏  举报