models:
class Book(models.Model):
name = models.CharField(max_length=32, verbose_name="书名")
#定义价格:
price = models.DecimalField(max_digits=5,decimal_places=2) #最大是99999.99元
#定义销量:
sale = models.IntegerField(default=0)
#定义库存:
kucun = models.IntegerField(default=100)
pub = models.ForeignKey(Publisher, null=True, on_delete=models.CASCADE, related_name='books',
related_query_name='book')
F和Q:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE","about_orm.settings")
import django
django.setup()
from app01 import models
from django.db.models import F,Q
#拿到库存:
ret = models.Book.objects.filter(sale__gt=F("kucun"))
#让销量乘以2:
ret = models.Book.objects.update(sale=F("sale")*2)
#拿到pk大于3且小于2的用逗号,或用|,&是与,~是非:
ret = models.Book.objects.filter(Q(pk__gt=3)|Q(pk__lt=2))