-
F对象
字段之间的比较
-
Q的逻辑运算
多个过滤器逐个调用表示逻辑与关系,同sql语句中where部分的and关键字
除了Book.object.filter(-Q(字段=值))
from django.db.models import Q
from bookstore.models import Author
#除了姓王的作者 或大于20
Author.objects.filter(-Q(name__startwith='王') | Q(age__gt=20))
-
raw
books=Book.objects.raw('select * from bookstore')
for book in books:
print(book)
-
cursor
跨过模型直接操作数据库
from django.db import connection
with connection.cursor() as cur:
#cur.execute('执行sqkl语句')
cur.execute('select * from book where id=1')
cur.fetchone()#查询一条结果 fetchall查询全部结果
cur.close()