亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Django分組聚合查詢實(shí)例分享

 更新時(shí)間:2020年04月29日 08:57:06   作者:Afrafre  
在本篇文章里小編給大家分享的是關(guān)于Django分組聚合查詢實(shí)例內(nèi)容,需要的朋友們可以參考下。

多表查詢

1. 增刪改

一對(duì)多:先一后多,外鍵可以為對(duì)象或依賴表的主鍵(publish and book)

publish = Publish.objects.create()
Book.objects.create(....publish=publish|publish_id=publish.id)

刪: 默認(rèn)存在級(jí)聯(lián)刪除

改: book修改外鍵,外鍵一定存在

多對(duì)多:

關(guān)系表的獲?。╞ook(主鍵) and author) book.author

增:book.author.add(作者對(duì)象們|主鍵們)

刪: clear()清除 remove() 可刪除單個(gè)作者

改: set([作者對(duì)象們|主鍵們])

2. 查

基于對(duì)象,正向找屬性,反向找類名小寫,多條記錄類名小寫_set

book.publish.first().name (book 一定是對(duì)象,不是queryset)
publish.book_set.first().name

基于雙下劃線:

Book.objects.filter(id=1).values('publish__name')[0] (values 查出的也是queryset)
publish.values('book__name')

今日內(nèi)容

1. 分組查詢: 聚合結(jié)果 group_by()

2. 聚合函數(shù)

3. 字段

分組查詢(單獨(dú)聚合查詢 and 分組聚合查詢---基于mysql)

Book: id name price publish_date publish

聚合函數(shù)可以單獨(dú)使用 ---- 整張表是一個(gè)大組

select max(price) from book

聚合函數(shù)在分組下使用

select max(price) as high_price from book group by publish having high_price > 50; 

聚合查詢---基于ORM

聚合函數(shù)的使用場景:

單獨(dú)使用:不分組,只查聚合結(jié)果

分組使用: 按字段分組,可查分組字段與聚合結(jié)果

導(dǎo)入聚合函數(shù):

from django.db.models import Avg,Max,Min,Count,Sum

單獨(dú)聚合查詢:aggregate (聚集,合集)---不分組

# 語法

# 聚合函數(shù): Max, Min,Sum, Avg, Count

aggregate(別名=聚合函數(shù)('字段‘)

規(guī)則:

1. 可以同時(shí)對(duì)多個(gè)字段進(jìn)行聚合處理: aggregate(name1= , name2= ...)

2. 是QuerySet 對(duì)象的方法(all,filter)

3. 返回值為dict類型

4. 在aggregate之前的values操作沒作用,被忽略

例: 所有書中最貴的書的價(jià)格

dic = Book.objects.all().aggregate(high_price=max('price),low_price=min('price'))

分組聚合查詢: annotate (注釋,做注解) --- 分組

# 語法
values('分組字段').annotate(別名=聚合函數(shù)(‘字段').filter(聚合別名條件).values('取分組字段','取聚合字段別名'))

規(guī)則:

1. values --- annotate 分組組合, values控制分組的字段,annotate控制聚合字段

2. values 可以按多個(gè)字段分組values('字段1‘,'字段2‘)

3. 可以同時(shí)對(duì)多個(gè)字段進(jìn)行聚合處理 annotate(別名1=max('price'),別名2=min('price'))

4. 分組后的filter 代表having判斷,只對(duì)聚合字段進(jìn)行條件判斷,(參數(shù)為非聚合或分組進(jìn)行條件判斷代表where判斷)

5. 取字段值 values() 省略默認(rèn)取所有分組字段和聚合字段,也可以自己定義(對(duì)非分組或非聚合字段,該字段自動(dòng)被變成分組字段)

# 案例:每個(gè)出版社出版的最貴的書的價(jià)格高于50元的出版社名與最高價(jià)格

# 思路:按出版社分組(從book出發(fā)),high_price=max('price'), filter(high_price__gt=50)

# 每個(gè)組的價(jià)格最貴的

Book.objects.all().values('publish__name').annotate(high_price=max('price').filter(high_price__gl=50).values('publish__name','high_price'))

字段屬性

1. null: 默認(rèn)Fasle(默認(rèn)字段不能為空) , True 表示字段可為null
2. blank: 默認(rèn)False, True 表示字段可以為空
3.choice: 限制了該選項(xiàng)字段值必須是指定的choice 中的一個(gè) (元組套元組)
sex = models.SmallIntegerField(choice=((1,'man'),(2,'female')))
obj.get_sex_display()

有choices 這個(gè)字段的: 要取得'女‘或'男‘, get_字段名sex_display() --超出失效
4. db_column: 自定義字段名
db_column='gender' 起別名該sex
5. db_index : True 設(shè)置索引
6. default: 字段默認(rèn)值
7. editable: 默認(rèn)為True, False: 不在 admin 界面顯示
8. primary_key : TRUE 為主鍵,
9. unique: true 字段值不可重復(fù)

字段

1. AutoField(): 默認(rèn)自增主鍵(primary_key=True)
2. BooleanField(): 布爾字段, 對(duì)應(yīng)database tinyint 類型
3. CharField(): 字符類型(默認(rèn)不為空)
max_length=20,null=True 可以為空
4. DateField(): 年月日
auto_now = True 數(shù)據(jù)別更新就會(huì)更新時(shí)間
auto_now_add = True 數(shù)據(jù)第一次產(chǎn)生時(shí)
5. DateTimeField(): 年月日時(shí)分秒
auto_now = True 數(shù)據(jù)別更新就會(huì)更新時(shí)間
auto_now_add = True 數(shù)據(jù)第一次產(chǎn)生時(shí)
6. DecimalField(): 混合精度的小數(shù)類型
max_digits = 5, 含小數(shù)為的最大位數(shù)
decimal_places = 2 , 小數(shù)位數(shù)
7. IntegerField(): 整型

不常用字段

關(guān)系字段

1. ForeignKey(): 外鍵字段
to= 關(guān)聯(lián)模型類 (一對(duì)多)
to_file = 關(guān)聯(lián)字段,省略默認(rèn)關(guān)聯(lián)主鍵
on_delete (外鍵關(guān)聯(lián)數(shù)據(jù)被刪除時(shí)的操作)
models.CASCADE 級(jí)聯(lián)刪除
modles.PROTECT 拋出異常
models.SET_NULL 設(shè)置空值
modles.SET_DEFAULT 設(shè)置默認(rèn)值
models.SET(value) 自定義值
related_name 自定義反向查詢的字段名
db_constraint=False, 取消關(guān)聯(lián),但還可以使用鏈表查詢
總結(jié): models.ForeignKey(to='related class name', null=True,on_delete=models.SET_NULL,db_constraint=False,related_name='本類名小寫')
2. OneToOneField(): 一對(duì)一字段
同外鍵
3, ManyToManyField() :多對(duì)多關(guān)系
to = 關(guān)聯(lián)模型類
through=關(guān)聯(lián)關(guān)系類
through_fields關(guān)聯(lián)關(guān)系表中(本身字段,關(guān)聯(lián)字段)

斷開外鍵關(guān)聯(lián)的ForeignKey使用(一對(duì)多,一對(duì)一)

# 一對(duì)多查詢 ----(publish and book)
# 方式一 : 不使用外鍵,在book 中添加 publish_id 屬性
# 不在支持Django ORM 鏈表查詢語法

# class Book(models.Model):
# name = models.CharField(max_length=20)
# publish_id = models.IntegerField(null=True)
#
# class Publish(models.Model):
# name = models.CharField(max_length=20)
#
# # 查詢方式:
# # 通過第一本書book 找出版社
# # id = Book.objects.first().publish_id
# # publish = Publish.objects.filter(id=id)[0].name
# # print(publish)

方式二:使用外鍵, 用db_constrain=False 字段段開連接
# 可以使用Django ORM連表查詢語法
class Book(models.Model):
name = models.CharField(max_length=20)
publish = models.ForeignKey(to='Publish',db_constraint=False,null=True,on_delete=models.SET_NULL) # to_field='id' 不寫會(huì)自動(dòng)添加

class Publish(models.Model):
name = models.CharField(max_length=20)

# 書的出版社 (外鍵方式)
# print(Book.objects.first().publish.name)
# print(Book.objects.filter(pk=1).values('publish__name'))

斷開關(guān)聯(lián)--- 多對(duì)多自動(dòng)創(chuàng)建關(guān)系表

# 斷開關(guān)聯(lián)(db_constraint屬性)的多對(duì)多自動(dòng)創(chuàng)建關(guān)系表 (book(外鍵) and author)
# 斷開后依然支持Django ORMlianiao 查詢語法
# 當(dāng)新表中無需新加額外字段時(shí), 可以自動(dòng)創(chuàng)建
class MyBook(models.Model):
name = models.CharField(max_length=20)
# 這里會(huì)產(chǎn)生第三張表
book_author = models.ManyToManyField(to='MyAuthor',db_constraint=False)

class MyAuthor(models.Model):
name = models.CharField(max_length=20)


# # 查詢方法
# # 多對(duì)多(自動(dòng)創(chuàng)建第三張表): 書的作者
# b1 = MyBook.objects.first()
# # b1.book_author 這個(gè)是關(guān)系表
# for author in b1.book_author.all():
# print(author.name)

# print(MyBook.objects.filter(pk=1).values('book_author__name'))

斷開關(guān)聯(lián) --- 多對(duì)多手動(dòng)創(chuàng)建關(guān)系表

# 手動(dòng)創(chuàng)建關(guān)系表的原因: 可以擁有自身字段,可以通過關(guān)系表類名直接獲取第三張表

# 手動(dòng)創(chuàng)建關(guān)系表可以讓關(guān)系表可以擁有更多的自身的字段,同時(shí)通過關(guān)系表類名可以直接獲取第三張表
'''
# ****
# 1、和自動(dòng)建立關(guān)系表類似,依然支持Django ORM連表查詢語法(多對(duì)多借助關(guān)系表連表查詢)
class Book(models.Model):
name = models.CharField(max_length=20)

class Author(models.Model):
name = models.CharField(max_length=20)

class Book_Author(models.Model):
book = models.ForeignKey(to="Book", null=True, on_delete=models.SET_NULL, db_constraint=False)
author = models.ForeignKey(to='Author', null=True, on_delete=models.SET_NULL, db_constraint=False)
time = models.DateField()
'''

'''
# ****
2、手動(dòng)創(chuàng)建關(guān)系表,在關(guān)系表中用ForeignKey方式支持基于外鍵關(guān)系表的ORM連表查詢,同時(shí)明確ManyToManyField字段,所以也支持ORM正向方向連表查詢
-- db_constraint=False斷開關(guān)聯(lián)可以在ForeignKey或ManyToManyField任意一方完成
class Book(models.Model):
name = models.CharField(max_length=20)
# 明確through與through_fields,ManyToManyField才不會(huì)自動(dòng)建立關(guān)系表,沒有關(guān)聯(lián)關(guān)系后就不能再使用db_constraint字段屬性
author = models.ManyToManyField(to='Author', through='Book_Author', through_fields=('book_id', 'author_id'))

class Author(models.Model):
name = models.CharField(max_length=20)

class Book_Author(models.Model):
book = models.ForeignKey(to="Book", null=True, on_delete=models.SET_NULL, db_constraint=False)
author = models.ForeignKey(to='Author', null=True, on_delete=models.SET_NULL, db_constraint=False)
time = models.DateField()
'''
# 總結(jié):手動(dòng)創(chuàng)建第三張表,第三張表的增刪改就采用關(guān)系表類名衍生的create|delete|update,就不再擁有add|clear|remove|set(因?yàn)殛P(guān)系表擁有自己的字段,這些方法無法直接操作這些字段)

到此這篇關(guān)于Django分組聚合查詢實(shí)例分享的文章就介紹到這了,更多相關(guān)Django分組聚合查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論