琛ㄦ搷浣滃強瀛楁鏌ヨ

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了琛ㄦ搷浣滃強瀛楁鏌ヨ相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/and' title='and'>and   杩斿洖   鏃犻檺   绾ц仈   鍗曡〃   aic   rgs   澶氫釜   鍒楄〃   

妯″瀷灞?/h2>

鍗曡〃鎿嶄綔

1. all(): 鏌ヨ鎵€鏈夌粨鏋?/strong>

鎶€鏈浘鐗? src=

2. filter(**kwargs): 瀹冨寘鍚簡涓庢墍缁欑瓫閫夋潯浠剁浉鍖归厤鐨勫璞?/strong>

鎶€鏈浘鐗? src=

3.get(**kwargs): 杩斿洖涓庢墍缁欑瓫閫夋潯浠剁浉鍖归厤鐨勫璞★紝杩斿洖缁撴灉鏈変笖鍙湁涓€涓紝濡傛灉绗﹀悎绛涢€夋潯浠剁殑瀵硅薄瓒呰繃涓€涓垨鑰呮病鏈夐兘浼氭姏鍑洪敊璇€?/strong>

鎶€鏈浘鐗? src=

鎶€鏈浘鐗? src=

4. exclude(**kwargs): 瀹冨寘鍚簡涓庢墍缁欑瓫閫夋潯浠朵笉鍖归厤鐨勫璞°€傜被浼间簬鍙栧弽銆?/strong>
鎶€鏈浘鐗? src=

5. values(*field): 杩斿洖涓€涓猇alueQuerySet——涓€涓壒娈婄殑QuerySet锛岃繍琛屽悗寰楀埌鐨勫苟涓嶆槸涓€绯诲垪model鐨勫疄渚嬪寲瀵硅薄锛岃€屾槸涓€涓彲杩唬鐨勫瓧鍏稿簭鍒?/strong>
鎶€鏈浘鐗? src=

6.values_list(*field): 瀹冧笌values()闈炲父鐩镐技锛屽畠杩斿洖鐨勬槸涓€涓厓缁勫簭鍒楋紝values杩斿洖鐨勬槸涓€涓瓧鍏稿簭鍒?/strong>
鎶€鏈浘鐗? src=

7.order_by(*field): 瀵规煡璇㈢粨鏋滄帓搴?/strong>

print(models.Book.objects.order_by(鈥榩rice鈥?)  # 榛樿鏄崌搴?
print(models.Book.objects.order_by(鈥?price鈥?)  # 鍔犺礋鍙峰氨鏄檷搴?/pre>

8.reverse(): 瀵规煡璇㈢粨鏋滃弽鍚戞帓搴忥紝鍓嶉潰瑕佸厛鏈夋帓搴忔墠鑳藉弽鍚?/strong>

9. distinct(): 浠庤繑鍥炵粨鏋滀腑鍓旈櫎閲嶅绾綍銆傚幓閲嶇殑鍓嶆彁鏄竴瀹氳鏈夊畬鍏ㄩ噸澶嶇殑璁板綍銆?/strong>
10.count(): 杩斿洖鏁版嵁搴撲腑鍖归厤鏌ヨ(QuerySet)鐨勫璞℃暟閲忋€?/strong>
鎶€鏈浘鐗? src=

11. first(): 杩斿洖绗竴鏉¤褰?/strong>
鎶€鏈浘鐗? src=

12.last(): 杩斿洖鏈€鍚庝竴鏉¤褰?/strong>

print(models.Book.objects.filter(pk=1).first())

13. exists(): 濡傛灉QuerySet鍖呭惈鏁版嵁锛屽氨杩斿洖True锛屽惁鍒欒繑鍥濬alse

鎿嶄綔鍒嗙被锛?/h3>

杩斿洖QuerySet瀵硅薄鐨勬柟娉曟湁锛?/strong>

  1. all()
  2. filter()
  3. exclude()
  4. order_by()
  5. reverse()
  6. distinct()

鐗规畩鐨凲uerySet

  1. values()       杩斿洖涓€涓彲杩唬鐨勫瓧鍏稿簭鍒?/li>
  2. values_list() 杩斿洖涓€涓彲杩唬鐨勫厓绁栧簭鍒?/li>

杩斿洖鍏蜂綋瀵硅薄鐨?/strong>

  1. get()
  2. first()
  3. last()

杩斿洖甯冨皵鍊肩殑鏂规硶鏈夛細

  1. exists()

杩斿洖鏁板瓧鐨勬柟娉曟湁

  1. count()

鍗曡〃鎿嶄綔涔嬪弻涓嬫粦鏌ヨ

鏌ョ湅orm鍐呴儴sql璇彞鐨勬柟娉曟湁浠ヤ笅鍑犵偣锛?/p>

銆€銆€1.濡傛灉鏄痲ueryset瀵硅薄锛岄偅涔堟垜浠彲浠ヤ娇鐢ㄧ偣query鏂规硶鐩存帴鏌ョ湅璇ueryset鐨勫唴閮╯ql璇彞銆?/p>

銆€銆€2.鍦╠jango椤圭洰鐨勯厤缃枃浠朵腑锛岄厤缃竴涓嬪弬鏁板嵆鍙疄鐜版墍鏈夌殑orm鍦ㄦ煡璇㈢殑鏃跺€欒嚜鍔ㄦ墦鍗板搴旂殑sql璇彞銆傚彲鐩存帴鎶婁笅闈㈢殑杩欐浠g爜澶嶅埗鍒皊ettings鏂囦欢涓€?/p>

 1 LOGGING = {
 2     鈥?/span>version鈥?/span>: 1,
 3     鈥?/span>disable_existing_loggers鈥?/span>: False,
 4     鈥?/span>handlers鈥?/span>: {
 5         鈥?/span>console鈥?/span>:{
 6             鈥?/span>level鈥?/span>:鈥?/span>DEBUG鈥?/span>,
 7             鈥?/span>class鈥?/span>:鈥?/span>logging.StreamHandler鈥?/span>,
 8     },
 9     },
10     鈥?/span>loggers鈥?/span>: {
11         鈥?/span>django.db.backends鈥?/span>: {
12             鈥?/span>handlers鈥?/span>: [鈥?/span>console鈥?/span>],
13             鈥?/span>propagate鈥?/span>: True,
14             鈥?/span>level鈥?/span>:鈥?/span>DEBUG鈥?/span>,
15         },
16     }
17 }    
models.Book.objects.filter(price__gt=200)銆€銆€# 鏌ヨ浠锋牸澶т簬200鐨勪功绫?/span>
__lt(灏忎簬)锛宊_gte(澶т簬绛変簬)锛宊_lte(灏忎簬绛変簬)

models.Book.objects.filter(price__in=[200,300,666.66])銆€銆€# 鑾峰彇price鏄?00鎴?00鎴?00.
models.Book.objects.filter(price__range=(200,800))銆€銆€#鑾峰彇price鍦?00鍒?00涔嬮棿鐨勶紝鍏朵腑鍖呮嫭200鍜?00.

# 鏌ヨ涔︾睄鍚嶅瓧涓寘鍚玴鐨?/span>
# 鍦ㄥ師鐢焥ql璇彞涓紝瑕佷娇鐢ㄦā绯婂尮閰?like锛?锛?)銆?/span>
models.Book.objects.filter(title__contains=鈥?/span>p鈥?/span>)  # 浠呬粎鍙兘鑾峰彇title瀛楁鍖呭惈"ven"鐨?/span>
models.Book.objects.filter(title__icontains=鈥?/span>p鈥?/span>)  # icontains瀵瑰ぇ灏忓啓涓嶆晱鎰?/span>

models.Book.objects.filter(title__startswith=鈥?/span>涓?/span>鈥?/span>)銆€銆€# 鑾峰彇title涓互涓夊紑澶寸殑
models.Book.objects.filter(title__endswith=鈥?/span>涓?/span>鈥?/span>)銆€銆€# 鑾峰彇title涓互涓夌粨灏剧殑

models.Book.objects.filter(create_time__year=鈥?/span>2017鈥?/span>)銆€銆€# 鏌ョ湅鍑虹増鏃ユ湡鐨?017骞寸殑

澶氳〃鎿嶄綔

鍥句功绠$悊绯荤粺琛ㄥ垱寤?/h3>

銆€銆€涓€瀵瑰:ForeignKey

銆€銆€涓€瀵逛竴:OnoToOneField锛屽彲浠ョ敤ForeignKey浠f浛ForeignKey(unique=True)

銆€銆€娉ㄦ剰锛氫笂闈袱涓叧閿瓧鎵€鍒涘缓鍑烘潵鐨勫瓧娈典細鑷姩鍔犱笂_id鍚庣紑

銆€銆€澶氬澶?ManyToManyFiled

銆€銆€銆€銆€璇ュ瓧娈靛苟涓嶄細鐪熸鐨勫湪琛ㄤ腑灞曠ず鍑烘潵锛屽畠浠呬粎鏄竴涓櫄鎷熷瓧娈碉紝瀹冪殑鍔熻兘鏈変袱澶х被銆?/p>

銆€銆€銆€銆€1.鍛婅瘔orm鑷姩鍒涘缓绗笁绉嶈〃銆?/p>

銆€銆€銆€銆€2.甯姪orm瀹炵幇璺ㄨ〃鏌ヨ銆?/p>

娴嬭瘯鐜鐨勪唬鐮佸涓嬪浘锛?/strong>

鎶€鏈浘鐗? src=

涓€瀵瑰瀛楁鐨勫鍒犳敼鏌?/h3>

澧?鍏抽敭瀛椾负create()    

鎶€鏈浘鐗? src=

 鏀?鍏抽敭瀛椾负update()

鎶€鏈浘鐗? src=

 鍒?鍏抽敭瀛椾负delete()

models.Publish.objects.filter(pk=2).delete()  # 榛樿閮芥槸绾ц仈鏇存柊 绾ц仈鍒犻櫎

澶氬澶氬瓧娈电殑澧炲垹鏀规煡

澧?鍏抽敭瀛椾负create()  

鎶€鏈浘鐗? src=

 鏀?鍏抽敭瀛椾负update()

鎶€鏈浘鐗? src=

 鍒?鍏抽敭瀛椾负remove()

鎶€鏈浘鐗? src=

#灏嗘煇鏈功璺熶綔鑰呯殑鍏崇郴鍏ㄩ儴娓呯┖
book_obj = models.Book.objects.filter(pk=1).first()
book_obj.authors.clear()  # 娓呯┖褰撳墠涔︾睄涓庝綔鑰呯殑鎵€鏈夊叧绯?/span>

 

銆€銆€灏忕粨锛歛dd()锛宻et()锛宺emove()閮芥敮鎸佹嫭鍙峰唴浼犳暟瀛楁垨鑰呭璞★紝骞朵笖鍙互浼犲涓紝浣嗘槸set闇€瑕佷紶涓€涓彲杩唬瀵硅薄銆俢lear()鍐呯殑鎷彿涓嶉渶瑕佺┛鍙傛暟銆?/p>

璺ㄨ〃鏌ヨ

锛?锛?涓€瀵逛竴鏌ヨ

銆€銆€姝e悜锛歛uthor---鍏宠仈瀛楁鍦╝uthor琛ㄩ噷--->authordetail 鎸夊瓧娈?/p>

銆€銆€鍙嶅悜锛歛uthordetail---鍏宠仈瀛楁鍦╝uthor琛ㄩ噷--->author 鎸夎〃鍚嶅皬鍐?/p>

锛?锛?涓€瀵瑰鏌ヨ

銆€銆€姝e悜锛歜ook---鍏宠仈瀛楁鍦╞ook琛ㄩ噷--->publish 鎸夊瓧娈?/p>

銆€銆€鍙嶅悜锛歱ublish---鍏宠仈瀛楁鍦╞ook琛ㄩ噷--->book 鎸夎〃鍚嶅皬鍐檁set.all() 鍥犱负涓€涓嚭鐗堢ぞ瀵瑰簲鐫€澶氫釜鍥句功
锛?锛? 澶氬澶氭煡璇?/p>

銆€銆€姝e悜锛歜ook---鍏宠仈瀛楁鍦╞ook琛ㄩ噷--->author 鎸夊瓧娈?/p>

銆€銆€鍙嶅悜锛歛uthor---鍏宠仈瀛楁鍦╞ook琛ㄩ噷--->book 鎸夎〃鍚嶅皬鍐檁set.all() 鍥犱负涓€涓綔鑰呭搴旂潃澶氫釜鍥句功

銆€銆€灏忕粨锛氭鍚戞煡璇㈡寜澶栭敭瀛楁锛屽弽鍚戞煡璇㈡寜琛ㄥ悕灏忓啓銆傞兘鏄熀浜庡璞$殑璺ㄨ〃鏌ヨ(瀛愭煡璇?灏嗕竴寮犺〃鐨勬煡璇㈢粨鏋滃綋鍋氬彟澶栦竴涓煡璇㈣鍙ョ殑鏉′欢)

銆€銆€寮鸿皟锛?/span>鍦ㄤ功鍐檕rm璇彞鐨勬椂鍊?璺熷啓sql璇彞涓€鏍凤紝涓嶈灏濊瘯鐫€涓€娆℃€у啓瀹岋紝搴旇鍋氬埌鍐欎竴鐐圭湅涓€鐐瑰啀鍐欎竴鐐广€?/p>

姝e悜鏌ユ壘鍙嶅悜鏌ユ壘

 

# 1.鏌ヨ鎵嬫満鍙锋槸130鐨勪綔鑰呭勾榫?/span>
# 姝e悜
#res=models.AuthorDetail.objects.filter(phone=130).values(鈥榓uthor__age鈥?
# print(res)

# 鍙嶅悜
#res1=models.Author.objects.filter(author_detail__phone=130).values(鈥榓ge鈥?
# print(res1)

# 2.鏌ヨ涔︾睄id鏄? 鐨勪綔鑰呯殑鐢佃瘽鍙风爜
# res = models.Book.objects.filter(pk=1).values(鈥榓uthors__author_detail__phone鈥?
# res1 = models.Book.objects.filter(pk=1).values(鈥樺閿瓧娈?__澶栭敭瀛楁2__澶栭敭瀛楁3__鏅€氬瓧娈碘€?
# print(res)  
# 鍙琛ㄩ噷闈㈡湁澶栭敭瀛楁 浣犲氨鍙互鏃犻檺鍒惰法澶氬紶琛?/span>

 

 

 

 

 

 

鑱氬悎鏌ヨ(aggregate)

銆€銆€aggregate()鏄?tt class="docutils literal">QuerySet 鐨勪竴涓粓姝㈠瓙鍙ャ€傜敤鍒扮殑鍐呯疆鍑芥暟鏈堿vg, Sum, Max, Min, Count銆?/p>

鎶€鏈浘鐗? src=

鍒嗙粍鏌ヨ

# 3.缁熻姣忎竴鏈功鐨勪綔鑰呬釜鏁?/span>
from django.db.models import Max, Min, Count, Avg, Sum
# res = models.Book.objects.annotate(author_num = Count(鈥榓uthors鈥?).values(鈥榓uthor_num鈥?鈥榯itle鈥?
# print(res)

# 4.缁熻鍑烘瘡涓嚭鐗堢ぞ鍗栫殑鏈€渚垮疁鐨勪功鐨勪环鏍?/span>
# res = models.Publish.objects.annotate(mmp = Min(鈥榖ook__price鈥?).values(鈥榥ame鈥?鈥榤mp鈥?
# print(res)

# 5.缁熻涓嶆涓€涓綔鑰呯殑鍥句功
# res = models.Book.objects.annotate(author_num=Count(鈥榓uthors鈥?).filter(author_num__gt=1)
# print(res)

#鍙鏄痲ueryset瀵硅薄 灏卞彲浠ユ棤闄愬埗鐨勮皟鐢╭ueryset瀵硅薄鐨勬柟娉?!!鏈€鏈€甯哥敤鐨勫氨鏄涓€涓凡缁廸ilter杩囨护瀹岀殑鏁版嵁 鍐嶈繘琛屾洿缁嗗寲鐨勭瓫閫?/span>

 

 

F鏌ヨ鍜孮鏌ヨ

F鏌ヨ

銆€銆€F鍙互甯垜浠彇鍒拌〃涓煇涓瓧娈靛搴旂殑鍊兼潵褰撲綔鎴戠殑绛涢€夋潯浠讹紝鑰屼笉鏄垜浠涓鸿緭鍏ョ殑锛屽疄鐜颁簡鍔ㄦ€佹瘮杈冪殑鏁堟灉銆?span style="color: #ff0000;">F鏌ヨ鐨勬湰璐ㄥ氨鏄粠鏁版嵁搴撲腑鑾峰彇鏌愪釜瀛楁鐨勫€笺€?/span>

# 1.鎶婃墍鏈変功鍚嶅悗闈㈠姞涓娾€樻柊娆锯€?
from django.db.models.functions import Concat
from django.db.models import Value
ret3=models.Product.objects.update(name=Concat(F(鈥?/span>name鈥?/span>),Value(鈥?/span>鏂版鈥?/span>)))
#  Concat琛ㄧず杩涜瀛楃涓茬殑鎷兼帴鎿嶄綔锛屽弬鏁颁綅缃喅瀹氫簡鎷兼帴鏄湪澶撮儴鎷兼帴杩樻槸灏鹃儴鎷兼帴锛孷alue閲岄潰鏄鏂板鐨勬嫾鎺ュ€?/span>

 

Q鏌ヨ

銆€銆€filter() 绛夋柟娉曚腑閫楀彿闅斿紑鐨勬潯浠舵槸涓庣殑鍏崇郴銆?nbsp;濡傛灉浣犻渶瑕佹墽琛屾洿澶嶆潅鐨勬煡璇紝灏卞彲浠ヤ娇鐢?tt class="xref py py-class docutils literal">Q瀵硅薄銆?/span>

# 1.鍗栧嚭鏁板ぇ浜?00 鎴栬€?浠锋牸灏忎簬100鍧楃殑
from django.db.models import Q
models.Product.objects.filter(Q(maichu__gt=100)|Q(price__lt=100))
#  瀵规潯浠跺寘瑁逛竴灞俀鏃跺€欙紝filter鍗冲彲鏀寔浜ゅ弶骞剁殑姣旇緝绗?/span>

銆€銆€鎴戜滑鍙互缁勫悎& 鍜?tt class="docutils literal">|  鎿嶄綔绗︿互鍙婁娇鐢ㄦ嫭鍙疯繘琛屽垎缁勬潵缂栧啓浠绘剰澶嶆潅鐨?tt class="docutils literal">Q 瀵硅薄銆傚悓鏃讹紝Q 瀵硅薄鍙互浣跨敤~ 鎿嶄綔绗﹀彇鍙嶏紝杩欏厑璁哥粍鍚堟甯哥殑鏌ヨ鍜屽彇鍙?NOT) 鏌ヨ銆?/p>

Q楂樼骇鐢ㄦ硶

q = Q()
q.connector = 鈥?/span>or鈥?/span>  # 淇敼鏌ヨ鏉′欢鐨勫叧绯?  榛樿鏄痑nd
q.children.append((鈥?/span>title__contains鈥?/span>,鈥?/span>涓夊浗婕斾箟鈥?/span>))  # 寰€鍒楄〃涓坊鍔犵瓫閫夋潯浠?/span>
q.children.append((鈥?/span>price__gt鈥?/span>,444))  # 寰€鍒楄〃涓坊鍔犵瓫閫夋潯浠?/span>
res = models.Book.objects.filter(q)  # filter鏀寔浣犵洿鎺ヤ紶q瀵硅薄  浣嗘槸榛樿杩樻槸and鍏崇郴
print(res)

 

以上是关于琛ㄦ搷浣滃強瀛楁鏌ヨ的主要内容,如果未能解决你的问题,请参考以下文章

鐩稿鍐呭瓨鐨勬枃浠舵搷浣滃瓧鑺傛祦

SUMSQ鍑芥暟璁$畻鏁板€煎钩鏂瑰拰

濡備綍璇嗗埆Form瀛楁涓竴瀵瑰鎴栬€呭瀵瑰瀛楁

MySQL瀛楁绫诲瀷 绾︽潫