F函数,用于操作对象的某一列的值
from django.db.models import F, Q
from .models import Fruit
fruit = Fruit.objects.get(id=1)
fruit.count += 1 等同于 fruit.count= F("count") + 1
Q函数, 对对象进行复杂查询, 并支持&(and),|(or),~(not)操作符。
from django.db.models import F, Q
from .models import Fruit
fruit = Fruit.objects.get(Q(name=‘apple‘)&Q(price__lt=30))
如果查询使用中带有关键字查询,Q对象一定要放在前面
fruit = Fruit.objects.get(Q(name=‘apple‘)&Q(price__lt=30), category=‘normal‘)