F and Q:

models:

Book class (models.Model): 
name = models.CharField (= 32 MAX_LENGTH, the verbose_name = "title")
#define Price:
. price = models.DecimalField (max_digits =. 5, decimal_places = 2) is the maximum # 99999.99 yuan
#define sales:
would like to purchase = models.IntegerField (default = 0)
# define inventory:
kucun = models.IntegerField (default = 100)
Pub = models.ForeignKey (Publisher, null = True, on_delete = models.CASCADE, the related_name = 'Books',
= related_query_name 'Book')

F. and Q:
os Import 
os.environ.setdefault ( "DJANGO_SETTINGS_MODULE", "about_orm.settings")
Import Django
django.setup ()
from app01 Import Models
from django.db.models Import F, Q
# get stock:
RET = models.Book. objects.filter (sale__gt = F. ( "kucun"))
# make sales multiplied by 2:
RET = models.Book.objects.update (Sale = F. ( "Sale") * 2)
# pk get greater than 3 and less than 2 a comma, or by |, and & is, non ~:
RET = models.Book.objects.filter (Q (= pk__gt. 3) | Q (pk__lt = 2))

Guess you like

Origin www.cnblogs.com/zhang-da/p/12103584.html