当前位置:网站首页>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))
边栏推荐
- 题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
- Selection of slow motion function
- 力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
- MySQL reported an error datetime (0) null
- P2102 地砖铺设(dfs&贪心)
- Sentinel sliding window traffic statistics
- 满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
- 2327. Number of people who know secrets (recursive)
- A blog to achieve embedded entry
- 【HBZ分享】ArrayList的增删慢查询快的原因
猜你喜欢
Fedora/REHL 安装 semanage
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
Embedded development program framework
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
DMA use of stm32
11. Intranet penetration and automatic refresh
The most detailed and comprehensive update content and all functions of guitar pro 8.0
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
Recommendation system (IX) PNN model (product based neural networks)
Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
随机推荐
Hashlimit rate control
Easyrecovery reliable and toll free data recovery computer software
团队协作出了问题,项目经理怎么办?
最高法院,离婚案件判决标准
Dry goods collection | Vulkan game engine video tutorial
【Try to Hack】john哈希破解工具
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
BOM - location, history, pop-up box, timing
ETCD数据库源码分析——etcdserver bootstrap初始化存储
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
How to realize automatic playback of H5 video
动态规划(树形dp)
Microservice resource address
比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
Fedora/REHL 安装 semanage
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Lambda expression learning
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
NPM command -- install dependent packages -- Usage / explanation