Django中Modual資料查詢操作

2021-09-10 09:22:35 字數 4278 閱讀 5381

本文以乙個影視資料庫為資料來源

模型類:

作為演示sql,定義乙個輸出sql語句的方法

def showsql():

from django.db import connection

queries = connection.queries

print(queries[-1]['sql'])

方法演示(第一條為django語句,第二條為對應sql語言):

movie.objects.get(mid=150)    #查詢乙個,返回值:物件,查詢不到出 .doesnotexist 錯。

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mid` = 150

movie.objects.first()    #查詢第乙個,返回值:物件

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` order by `movie`.`mid` asc limit 1

movie.objects.last()    #查詢最後乙個,返回值:物件

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` order by `movie`.`mid` desc limit 1

movie.objects.count()    #查詢總條數,返回值:int

select count(*) as `__count` from `movie`

movie.objects.all()    #查詢所有,但預設只取前21條,返回值:queryset  ##懶載入

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` limit 21

movie.objects.all()[20:25]    #查詢20到25條(切片)

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` limit 5 offset 20

movie.objects.filter(mname='王牌保鏢')    #條件查詢,與get不同是的,它返回的是queryset集合,查詢不到不報錯

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` = '王牌保鏢' limit 21

模糊查詢(%、_、like):

movie.objects.filter(mname__endswith='保鏢')    #以什麼為結尾

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '%保鏢' limit 21

movie.objects.filter(mname__startswith='保鏢')    #以什麼開頭

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '保鏢%' limit 21

movie.objects.filter(mname__contains='保鏢')    #包含什麼

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '%保鏢%' limit 21

movie.objects.filter(mname__exact='保鏢')    #相等於什麼

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` = '保鏢' limit 21

movie.objects.filter(mname__istartswith='m')    #忽略大小寫

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like 'm%' limit 21

movie.objects.filter(mname__isnull=true)    #判斷該欄位是否為空

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` is null limit 21

movie.objects.filter(mname__contains='保鏢',mid=161)    #多條件查詢,後面逗號接語句

movie.objects.filter(mname__contains='保鏢').filter(mid=161)    #功能與上面相同

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where (`movie`.`mid` = 161 and `movie`.`mname` like binary '%保鏢%') limit 21

movie.objects.values('mname','mid').filter(mname__contains='保鏢')    #欄位名部分顯示

select `movie`.`mname`, `movie`.`mid` from `movie` where `movie`.`mname` like binary '%保鏢%' limit 21

movie.objects.filter(mname__contains='保鏢').exclude(mname__startswith='愛情')    #不等於什麼

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where (`movie`.`mname` like binary '%保鏢%' and not (`movie`.`mname` like binary '愛情%')) limit 21

movie.objects.filter(mname__contains='保鏢').order_by('mid')    #排序,預設為公升序, 在mid前加個"-"號則為降序,如:.order_by('-mid')

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '%保鏢%' order by `movie`.`mid` asc limit 21

注意:mname__contains、mname__istartswith

都是欄位名加雙下劃線來查詢。同也可以運用其它的方式來查詢如:gt、lt、et等等,如

movie.objects.filter(mid__gt=500).order_by('-mid')    #查詢mid大於等於500的資料

select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mid` > 500 order by `movie`.`mid` desc limit 21

Django中資料儲存,資料加密功能

1 cookie 1 會話技術 2 客戶端的會話技術 資料儲存在瀏覽器上 3 問題 導致原因 在web應用中,一次網路請求是從request開始,到response結束,跟以後的請求或者跟其他請求沒有關係 導致每次請求之間的資料沒有關係 短連線 長鏈結 解決 在客戶端保留資料 cookie 或者服務...

Django中資料庫配置

在settings.py中儲存了資料庫的連線配置資訊,django預設初始配置使用sqlite資料庫。databases 使用mysql資料庫首先需要安裝驅動程式 pip install pymysql 在django的工程同名子目錄的 init py檔案中新增如下語句 from pymysql i...

Django中資料庫的配置

最近再用django開發乙個專案,其中肯定少不了資料庫的操作,我自己用的主要還是mysql資料庫,我用的是mysql 5.4.40版本,但是基本上所有的資料庫操作連線都可以用。在這之前首先必須安裝python mysql,記得安裝對應的版本。之前我的做法是在檔案中寫資料庫的連線,沒有在setting...