-
使用extra:
models.Book.objects.filter(publisher__name='傳說中的申小五').extra(where=['price>50'])
models.Book.objects.filter(publisher__name='傳說中的申小五', price__gt=50)
models.Book.objects.extra(select={'count': 'select count(*) from hello_Book'}) -
使用raw:
Book.objects.raw('select * from hello_Book') # 返回模型實例 -
執行自定義SQL語言:
from django.db import connection
cursor=connection.cursor()
# 插入操作
cursor.execute("insert into hello_author(name) values('傳說中的申小五')")
# 更新操作
cursor.execute("update hello_author set name='abc' where name='bcd'")
# 刪除操作
cursor.execute("delete from hello_author where name='abc'")
# 查詢操作
cursor.execute("select * from hello_author")
raw=cursor.fetchone() # 返回結果行游標直讀向前,讀取一條
cursor.fetchall() # 讀取所有
