当前位置:网站首页>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))
边栏推荐
- Can Flink SQL read multiple topics at the same time. How to write in with
- VPP性能测试
- Embedded development program framework
- It is also a small summary in learning
- English Vocabulary - life scene memory method
- Luogu deep foundation part 1 Introduction to language Chapter 2 sequential structure programming
- P2102 地砖铺设(dfs&贪心)
- C'est un petit résumé de l'étude.
- Digital children < daily question> (Digital DP)
- SharedPreferences 源码分析
猜你喜欢
VNCTF2022 WriteUp
The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Solve the compilation problem of "c2001: line breaks in constants"
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
RTP GB28181 文件测试工具
Recommendation system (IX) PNN model (product based neural networks)
Visio draw fan
Coreldraw2022 new version new function introduction cdr2022
Sqlserver query results are not displayed in tabular form. How to modify them
随机推荐
Quatre méthodes de redis pour dépanner les grandes clés sont nécessaires pour optimiser
[FreeRTOS interrupt experiment]
P3500 [poi2010]tes intelligence test (two points & offline)
2328. Number of incremental paths in the grid graph (memory search)
P2022 有趣的数(二分&数位dp)
[face recognition series] | realize automatic makeup
Is the mode of education together - on campus + off campus reliable
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
ue5 小知识点 开启lumen的设置
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
The most detailed and comprehensive update content and all functions of guitar pro 8.0
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Easyrecovery靠谱不收费的数据恢复电脑软件
After learning classes and objects, I wrote a date class
E. Best Pair
Solve the compilation problem of "c2001: line breaks in constants"
The value of two date types is subtracted and converted to seconds
SQL注入漏洞(MSSQL注入)
hashlimit速率控制