琛ㄦ搷浣滃強瀛楁鏌ヨ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了琛ㄦ搷浣滃強瀛楁鏌ヨ相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/and' title='and'>and
杩斿洖 鏃犻檺 绾ц仈 鍗曡〃 aic rgs 澶氫釜 鍒楄〃妯″瀷灞?/h2>
鍗曡〃鎿嶄綔
1. all(): 鏌ヨ鎵€鏈夌粨鏋?/strong>
2. filter(**kwargs): 瀹冨寘鍚簡涓庢墍缁欑瓫閫夋潯浠剁浉鍖归厤鐨勫璞?/strong>
3.get(**kwargs): 杩斿洖涓庢墍缁欑瓫閫夋潯浠剁浉鍖归厤鐨勫璞★紝杩斿洖缁撴灉鏈変笖鍙湁涓€涓紝濡傛灉绗﹀悎绛涢€夋潯浠剁殑瀵硅薄瓒呰繃涓€涓垨鑰呮病鏈夐兘浼氭姏鍑洪敊璇€?/strong>
4. exclude(**kwargs): 瀹冨寘鍚簡涓庢墍缁欑瓫閫夋潯浠朵笉鍖归厤鐨勫璞°€傜被浼间簬鍙栧弽銆?/strong>
5. values(*field): 杩斿洖涓€涓猇alueQuerySet——涓€涓壒娈婄殑QuerySet锛岃繍琛屽悗寰楀埌鐨勫苟涓嶆槸涓€绯诲垪model鐨勫疄渚嬪寲瀵硅薄锛岃€屾槸涓€涓彲杩唬鐨勫瓧鍏稿簭鍒?/strong>
6.values_list(*field): 瀹冧笌values()闈炲父鐩镐技锛屽畠杩斿洖鐨勬槸涓€涓厓缁勫簭鍒楋紝values杩斿洖鐨勬槸涓€涓瓧鍏稿簭鍒?/strong>
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>
11. first(): 杩斿洖绗竴鏉¤褰?/strong>
12.last(): 杩斿洖鏈€鍚庝竴鏉¤褰?/strong>
print(models.Book.objects.filter(pk=1).first())
13. exists(): 濡傛灉QuerySet鍖呭惈鏁版嵁锛屽氨杩斿洖True锛屽惁鍒欒繑鍥濬alse
鎿嶄綔鍒嗙被锛?/h3>
杩斿洖QuerySet瀵硅薄鐨勬柟娉曟湁锛?/strong>
- all()
- filter()
- exclude()
- order_by()
- reverse()
- distinct()
鐗规畩鐨凲uerySet
- values() 杩斿洖涓€涓彲杩唬鐨勫瓧鍏稿簭鍒?/li>
- values_list() 杩斿洖涓€涓彲杩唬鐨勫厓绁栧簭鍒?/li>
杩斿洖鍏蜂綋瀵硅薄鐨?/strong>
- get()
- first()
- last()
杩斿洖甯冨皵鍊肩殑鏂规硶鏈夛細
- exists()
杩斿洖鏁板瓧鐨勬柟娉曟湁
- 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>
涓€瀵瑰瀛楁鐨勫鍒犳敼鏌?/h3>
澧?鍏抽敭瀛椾负create()
鏀?鍏抽敭瀛椾负update()
鍒?鍏抽敭瀛椾负delete()
models.Publish.objects.filter(pk=2).delete() # 榛樿閮芥槸绾ц仈鏇存柊 绾ц仈鍒犻櫎
澶氬澶氬瓧娈电殑澧炲垹鏀规煡
澧?鍏抽敭瀛椾负create()
鏀?鍏抽敭瀛椾负update()
鍒?鍏抽敭瀛椾负remove()
#灏嗘煇鏈功璺熶綔鑰呯殑鍏崇郴鍏ㄩ儴娓呯┖ 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悜鏌ユ壘鍙嶅悜鏌ユ壘 鑱氬悎鏌ヨ(aggregate) 銆€銆€aggregate()鏄?tt class="docutils literal">QuerySet 鐨勪竴涓粓姝㈠瓙鍙ャ€傜敤鍒扮殑鍐呯疆鍑芥暟鏈堿vg, Sum, Max, Min, Count銆?/p>
鍒嗙粍鏌ヨ 銆€銆€F鍙互甯垜浠彇鍒拌〃涓煇涓瓧娈靛搴旂殑鍊兼潵褰撲綔鎴戠殑绛涢€夋潯浠讹紝鑰屼笉鏄垜浠涓鸿緭鍏ョ殑锛屽疄鐜颁簡鍔ㄦ€佹瘮杈冪殑鏁堟灉銆?span style="color: #ff0000;">F鏌ヨ鐨勬湰璐ㄥ氨鏄粠鏁版嵁搴撲腑鑾峰彇鏌愪釜瀛楁鐨勫€笺€?/span> 銆€銆€filter() 绛夋柟娉曚腑閫楀彿闅斿紑鐨勬潯浠舵槸涓庣殑鍏崇郴銆?nbsp;濡傛灉浣犻渶瑕佹墽琛屾洿澶嶆潅鐨勬煡璇紝灏卞彲浠ヤ娇鐢?tt class="xref py py-class docutils literal">Q瀵硅薄銆?/span> 銆€銆€鎴戜滑鍙互缁勫悎& 鍜?tt class="docutils literal">| 鎿嶄綔绗︿互鍙婁娇鐢ㄦ嫭鍙疯繘琛屽垎缁勬潵缂栧啓浠绘剰澶嶆潅鐨?tt class="docutils literal">Q 瀵硅薄銆傚悓鏃讹紝Q 瀵硅薄鍙互浣跨敤~ 鎿嶄綔绗﹀彇鍙嶏紝杩欏厑璁哥粍鍚堟甯哥殑鏌ヨ鍜屽彇鍙?NOT) 鏌ヨ銆?/p>
# 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>
# 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鏌ヨ
# 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鏌ヨ
# 1.鍗栧嚭鏁板ぇ浜?00 鎴栬€?浠锋牸灏忎簬100鍧楃殑
from django.db.models import Q
models.Product.objects.filter(Q(maichu__gt=100)|Q(price__lt=100))
# 瀵规潯浠跺寘瑁逛竴灞俀鏃跺€欙紝filter鍗冲彲鏀寔浜ゅ弶骞剁殑姣旇緝绗?/span>
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)
以上是关于琛ㄦ搷浣滃強瀛楁鏌ヨ的主要内容,如果未能解决你的问题,请参考以下文章