models.Tb1.objects.filter(name='seven').count()
# models.Tb1.objects.filter(id__gt=1) # 获取id大于1的值
# models.Tb1.objects.filter(id__gte=1) # 获取id大于等于1的值
# models.Tb1.objects.filter(id__lt=10) # 获取id小于10的值
# models.Tb1.objects.filter(id__lte=10) # 获取id小于10的值
# models.Tb1.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值
# in
#
# models.Tb1.objects.filter(id__in=[11, 22, 33]) # 获取id等于11、22、33的数据
# models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in
# contains
#
# models.Tb1.objects.filter(name__contains="ven")
# models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感
# models.Tb1.objects.exclude(name__icontains="ven")
# range
#
# models.Tb1.objects.filter(id__range=[1, 2]) # 范围bettwen and
默认从小到大,加减号,是从大到小
多个字段进行排序
# models.Tb1.objects.filter(name='seven').order_by('id') # asc
# models.Tb1.objects.filter(name='seven').order_by('-id') # desc
from django.db.models import Count, Min, Max, Sum
我们想要以某一个字段进行分组,我们一般想到的MySQL语句是
select count(1) as count
from student group by age
那么Django如果实现以上的功能,不仅仅以一个字段进行分组,而且还给查询出的字段起别名
以上生成的sql语句是
SELECT `myfirst_article`.`category_id`,
COUNT(`myfirst_article`.`id`) AS `c`
FROM `myfirst_article` GROUP BY
`myfirst_article`.`category_id` ORDER BY NULL
整个表进行分组
from django.db.models import Count, Min, Max, Sum
# 聚合函数,获取字典类型聚合结果
from django.db.models import Count, Avg, Max, Min, Sum
result = models.UserInfo.objects.aggregate(
k=Count('u_id', distinct=True), n=Count('nid')
)
===> {'k': 3, 'n': 4}
Article.objects.exclude(id=1)
我们在做更新的时候,获取上一次的值 比如我们想要将一个字段的值加一,不需要每一次都拿出来加一之后再保存,可以这样写
# from django.db.models import F
# Tb1.objects.update(num=F('num')+1)
用这个函数,实现多个条件的查询,且 或关系的查询
组合搜索的时候,就会使用这个
# 方式一:
# Q(nid__gt=10)
# Q(nid=8) | Q(nid__gt=10)
# Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption='root')
# 方式二:
# con = Q()
# q1 = Q()
# q1.connector = 'OR'
# q1.children.append(('id', 1))
# q1.children.append(('id', 10))
# q1.children.append(('id', 9))
q1 对象里面的3个数据是或者关系
# q2 = Q()
# q2.connector = 'OR'
# q2.children.append(('c1', 1))
# q2.children.append(('c1', 10))
# q2.children.append(('c1', 9))
q2 对象里面的3个数据是或者关系
# con.add(q1, 'AND')
# con.add(q2, 'AND')
#
# models.Tb1.objects.filter(con)
使用
以上就这样写
# models.Tb1.objects.all()[10:20]
# date
#
# Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
# Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
# year
#
# Entry.objects.filter(pub_date__year=2005)
# Entry.objects.filter(pub_date__year__gte=2005)
# month
#
# Entry.objects.filter(pub_date__month=12)
# Entry.objects.filter(pub_date__month__gte=6)
# day
#
# Entry.objects.filter(pub_date__day=3)
# Entry.objects.filter(pub_date__day__gte=3)
# week_day
#
# Entry.objects.filter(pub_date__week_day=2)
# Entry.objects.filter(pub_date__week_day__gte=2)
# hour
#
# Event.objects.filter(timestamp__hour=23)
# Event.objects.filter(time__hour=5)
# Event.objects.filter(timestamp__hour__gte=12)
# minute
#
# Event.objects.filter(timestamp__minute=29)
# Event.objects.filter(time__minute=46)
# Event.objects.filter(timestamp__minute__gte=29)
# second
#
# Event.objects.filter(timestamp__second=31)
# Event.objects.filter(time__second=2)
# Event.objects.filter(timestamp__second__gte=31)
我们要实现以上的功能
他的参数是什么,我们看源码
观察源码,有好多的参数,我们下面解释这些参数的值可以如何写
shutype = Article.objects.all().extra(
select={'count':"select count(1) from myfirst_article"}
)
for item in shutype:
print(item.count)
以上查询条件生成的sql语句是
SELECT
(select count(1) from myfirst_article ) AS `count`,
`myfirst_article`.`id`,
`myfirst_article`.`title`,
`myfirst_article`.`content`,
`myfirst_article`.`category_id`
FROM `myfirst_article`
这个里面写的是具体的变量,就是我们的sql语句里面有些是变化的,我们可以用这个参数作为入参
shutype = Article.objects.all().extra(
select={'count':"select count(1) from myfirst_article
where id>%s"},select_params=[1]
)
for item in shutype:
print(item.count)
以上查询条件生成的sql语句是
SELECT
0
(select count(1) from myfirst_article where id>1) AS `count`,
`myfirst_article`.`id`,
`myfirst_article`.`title`,
`myfirst_article`.`content`,
`myfirst_article`.`category_id`
FROM `myfirst_article`
或者关联的查询条件
shutype = Article.objects.all().extra(
where=['id=%s or id=%s'], params=['1','2']
)
for item in shutype:
print(item.title)
也就是拼接where后面的条件 以上生成的sql语句是
SELECT
`myfirst_article`.`id`,
`myfirst_article`.`title`,
`myfirst_article`.`content`,
`myfirst_article`.`category_id`
FROM `myfirst_article` WHERE (id=1 or id=2)
and的查询条件
shutype = Article.objects.all().extra(
where=['id=%s ',' id=%s'], params=['1','2']
)
for item in shutype:
print(item.title)
以上生成的sql语句是
SELECT
`myfirst_article`.`id`,
`myfirst_article`.`title`,
`myfirst_article`.`content`,
`myfirst_article`.`category_id`
FROM `myfirst_article` WHERE (id=1 ) AND ( id=2)
SELECT
`myfirst_article`.`id`,
`myfirst_article`.`title`,
`myfirst_article`.`content`,
`myfirst_article`.`category_id`
FROM `myfirst_article`
WHERE (id=1 ) AND ( id=2)
ORDER BY `myfirst_article`.`id` DESC
shutype = Article.objects.all().extra(
tables=['myfirst_category'])
for item in shutype:
print(item.title)
查询多个表
SELECT
`myfirst_article`.`id`,
`myfirst_article`.`title`,
`myfirst_article`.`content`,
`myfirst_article`.`category_id`
FROM `myfirst_article` , `myfirst_category`
有的查询比较复杂,用原生sql语句是比较的简单
#
# from django.db import connection, connections
# cursor = connection.cursor() 这个是默认的数据源
Django支持多个数据源,用以下的写法选择某一个数据源
# cursor = connections['default'].cursor()
# cursor.execute("""SELECT * from auth_user where id = %s""", [1])
# row = cursor.fetchone()
def reverse(self):
# 倒序
models.UserInfo.objects.all().order_by('-nid').reverse()
# 注:如果存在order_by,reverse则是倒序,如果多个排序则一一倒序
用这个前面必须有order_by()函数
all()
value()
仅仅取出这个里面写的字段
以上这样写法,查询出的还是对象
取出除了这个里面的其他字段
查询出的还是对象
指定去哪个数据库拿数据
获取每行数据为字典格式
获取每行数据为元祖
# 如果存在,则获取,否则,创建
# defaults 指定创建时,其他字段的值
obj, created = models.UserInfo.objects.get_or_create(username='root1', defaults={'email': '1111111','u_id': 2, 't_id': 2})
# 如果存在,则更新,否则,创建
# defaults 指定创建时或更新时的其他字段
obj, created = models.UserInfo.objects.update_or_create(username='root1', defaults={'email': '1111111','u_id': 2, 't_id': 1})
# 根据主键ID进行查找
id_list = [11,21,31]
UserInfo.objects.in_bulk(id_list)