当前位置:网站首页>Orm-f & Q object
Orm-f & Q object
2022-07-06 04:40:00 【Legal procedures for working in banks】
1.F object
Modify the data without extracting the created object
without F object , According to the content of the previous course , We need to implement a like function :
1. Create an object , And read the base number of likes
2. On the created object +1
3. Save the object back to the database .
It seems ok , But at the same time 100 Individuals create objects and like , The base number is 10,100 The individual saves the theory into the database at the same time after he likes it , that 100 The data saved by individuals are 10+1, Probably 100 After people like it , The result is 11. amount to 100 The data submitted by individuals to the database is 11.
and F Object directly wraps the value of the database , Use F Object is equivalent to submitting data to this field +=1, Using the lock of the database will 100 Connect individuals . No existence 100 The situation that an individual takes out the field value and saves it . amount to 100=F Personal operation +1
from django.db.models import F
def add_like(request,topic_id):
topic = Topic.objects.get(id=topic_id)
topic.like = F(' Like ') + 1
topic.save()
In addition, you can compare database field values
from django.db.models import F
from bookstore.models import Book
books = Book.objects.filter(mark_price__gt=F('price'))
for book in books:
print(book.title,' pricing :',book.price,' Present price :',book.mark_price)
2.Q object
or | And & Not ~ Wait for the operation , quote django.db.models in
Such as
from django.db.models import Q
# Search pricing and cost are less than 20 Yuan's book
Book.objects.filter(Q(price__lt=20)|Q(mark_price__lt=20))

边栏推荐
- One question per day (Mathematics)
- C. The Third Problem(找规律)
- P3500 [POI2010]TES-Intelligence Test(二分&离线)
- tengine 内核参数
- CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)
- Tengine kernel parameters
- npm命令--安装依赖包--用法/详解
- Word cover underline
- BOM - location, history, pop-up box, timing
- 11. Intranet penetration and automatic refresh
猜你喜欢

Selection of slow motion function

How to estimate the population with samples? (mean, variance, standard deviation)

比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元

Visio draws Tai Chi

Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022

Coreldraw2022 new version new function introduction cdr2022

CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)

题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》

Distributed transaction solution

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
Digital children < daily question> (Digital DP)
CADD course learning (8) -- virtual screening of Compound Library
Redis has four methods for checking big keys, which are necessary for optimization
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
Lombok principle and the pit of ⽤ @data and @builder at the same time
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
[tomato assistant installation]
Can CDC pull the Oracle table in full
Basic explanation of turtle module - draw curve
View workflow
ISP学习(2)
CertBot 更新证书失败解决
DMA use of stm32
BOM - location, history, pop-up box, timing
Lagrange polynomial
P2102 floor tile laying (DFS & greed)
CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)
ue5 小知识点 开启lumen的设置
Programmers' position in the Internet industry | daily anecdotes
Word cover underline