django—model核心查询
<
1
>
all
(): 查询所有结果
<
2
>
filter
(
*
*
kwargs): 它包含了与所给筛选条件相匹配的对象
<
3
> get(
*
*
kwargs): 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,
如果符合筛选条件的对象超过一个或者没有都会抛出错误。
<
5
> exclude(
*
*
kwargs): 它包含了与所给筛选条件不匹配的对象
<
4
> values(
*
field): 返回一个ValueQuerySet——一个特殊的QuerySet,运行后得到的并不是一系列
model的实例化对象,而是一个可迭代的字典序列
<
9
> values_list(
*
field): 它与values()非常相似,它返回的是一个元组序列,values返回的是一个字典序列
<
6
> order_by(
*
field): 对查询结果排序
<
7
> reverse(): 对查询结果反向排序
<
8
> distinct(): 从返回结果中剔除重复纪录
<
10
> count(): 返回数据库中匹配查询(QuerySet)的对象数量。
<
11
> first(): 返回第一条记录
<
12
> last(): 返回最后一条记录
<
13
> exists(): 如果QuerySet包含数据,就返回
True
,否则返回
False
models.Tb1.objects.
filter
(id__lt
=
10
, id__gt
=
1
)
# 获取id大于1 且 小于10的值
models.Tb1.objects.
filter
(id__in
=
[
11
,
22
,
33
])
# 获取id等于11、22、33的数据
models.Tb1.objects.exclude(id__in
=
[
11
,
22
,
33
])
# not in
models.Tb1.objects.
filter
(name__contains
=
"ven"
)
models.Tb1.objects.
filter
(name__icontains
=
"ven"
)
# icontains大小写不敏感
models.Tb1.objects.
filter
(id__range
=
[
1
,
2
])
# 范围bettwen and
startswith,istartswith, endswith, iendswith
基于对象的跨表查询
一对多查询(Publish 与 Book)
正向查询(按字段:publish):
查询nid=1的书籍的出版社所在的城市<br>
book_obj
=
Book.objects.get(nid
=
1)
# book_obj.publish 是nid=1的书籍对象
publish
=
Publish.objects.get(name
=
"人民出版社"
)
book_list
=
publish.book_set.
all
()
# 与人民出版社关联的所有书籍对象集合
for
book_obj
in
book_list:
print
(book_obj.title)
author_egon
=
Author.objects.get(name
=
"egon"
)
print
(author_egon.authorDetail.telephone)
authorDetail_list
=
AuthorDetail.objects.
filter
(addr
=
"beijing"
)
for
obj
in
authorDetail_list:
print
(obj.author.name)
book_obj
=
Book.objects.
filter
(title
=
"金瓶眉"
).first()
authors
=
book_obj.authors.
all
()
for
author_obj
in
authors:
print
(author_obj.name,author_obj.authorDetail.telephone
author_obj
=
Author.objects.get(name
=
"egon"
)
book_list
=
author_obj.book_set.
all
()
#与egon作者相关的所有书籍
for
book_obj
in
book_list:
print
(book_obj.title)
基于双下划线的跨表查询
关键点:正向查询按字段,反向查询按表明。
练习1: 查询人民出版社出版过的所有书籍的名字与价格(一对多)
# 正向查询 按字段:publish
queryResult=Book.objects
.filter(publish__name="人民出版社")
.values_list("title","price")
# 反向查询 按表名:book
queryResult=Publish.objects
.filter(name="人民出版社")
.values_list("book__title","book__price")
聚合查询与分组查询
聚合:aggregate(*args, **kwargs)
计算所有图书的平均价格
>>>
from
django.db.models
import
Avg
>>> Book.objects.
all
().aggregate(Avg(
'price'
))
{
'price__avg'
:
34.35
}
>>> Book.objects.aggregate(average_price
=
Avg(
'price'
))
{
'average_price'
:
34.35
}
>>>
from
django.db.models
import
Avg,
Max
,
Min
>>> Book.objects.aggregate(Avg(
'price'
),
Max
(
'price'
),
Min
(
'price'
))
{
'price__avg'
:
34.35
,
'price__max'
: Decimal(
'81.20'
),
'price__min'
: Decimal(
'12.99'
)}
分组:annotate()
为调用的QuerySet中每一个对象都生成一个独立的统计值(统计方法用聚合函数)。
(1) 练习:统计每一本书的作者个数
bookList
=
Book.objects.annotate(authorsNum
=
Count(
'authors'
))
for
book_obj
in
bookList:
print
(book_obj.title,book_obj.authorsNum)
publishList
=
Publish.objects.annotate(MinPrice
=
Min
(
"book__price"
))
for
publish_obj
in
publishList:
print
(publish_obj.name,publish_obj.MinPrice)
(5) 根据一本图书作者数量的多少对查询集 QuerySet进行排序:
Book.objects.annotate(num_authors
=
Count(
'authors'
)).order_by(
'num_authors'
)
(6) 查询各个作者出的书的总价格:
按author表的所有字段 group by
queryResult=Author.objects
.annotate(SumPrice=Sum("book__price"))
.values_list("name","SumPrice")
print(queryResult)
# 按authors__name group by
queryResult2=Book.objects.values("authors__name")
.annotate(SumPrice=Sum("price"))
.values_list("authors__name","SumPrice")
print(queryResult2)
F查询与Q查询
F查询
在上面所有的例子中,我们构造的过滤器都只是将字段值与某个常量做比较。如果我们要对两个字段的值做比较,那该怎么做呢?
Django 提供 F() 来做这样的比较。F() 的实例可以在查询中引用字段,来比较同一个 model 实例中两个不同字段的值。
查询评论数大于收藏数的书籍
from
django.db.models
import
F
Book.objects.
filter
(commnetNum__lt
=
F(
'keepNum'
))
查询评论数大于收藏数2倍的书籍
Book.objects.
filter
(commnetNum__lt
=
F(
'keepNum'
)
*
2
)
Book.objects.
all
().update(price
=
F(
"price"
)
+
30
)
Q查询
Q 对象可以使用& 和| 操作符组合起来。当一个操作符在两个Q 对象上使用时,它产生一个新的Q 对象。
bookList
=
Book.objects.
filter
(Q(authors__name
=
"yuan"
)|Q(authors__name
=
"egon"
))
WHERE name
=
"yuan"
OR name
=
"egon"