当前位置:网站首页>ORM aggregate query and native database operation
ORM aggregate query and native database operation
2022-07-06 04:40:00 【Legal procedures for working in banks】
1. Aggregate query
Classification is divided into whole table aggregation and grouping aggregation .
1.1 Whole table aggregation
First import from django.db.models import *
Aggregate functions Sum Avg Count Max Min
grammar :
MyModel.objects.aggregate( Outcome variable = Aggregate functions (’ Column ‘))
Return dictionary :{ Result variable name : value }
1.2 Group aggregation
Group first , Repolymerization
1.2.1 Group first
MyModel.objects.values(‘ Column 1’,’ Column 2‘)
1.2.2 use annotate polymerization
2. Native database operations
prevent sql Inject , Not recommended
raw return RawQuerySet Just cycle
Inquire about :
res = Book.objects.raw('select * from bookstore_book where id =%s'%(id))
2.1 The cursor
Operate the database completely across model classes , Check, modify and delete
2.1.1 Import cursor
from django.db import connection
2.1.2 establish cursor object
Usually use with sentence , Easy to release cursor resources
from django.db import connection
with connection.cusor() as cur:
cur.exectue('SQL',' Splicing parameter ')
边栏推荐
- Dynamic programming (tree DP)
- What should the project manager do if there is something wrong with team collaboration?
- One question per day (Mathematics)
- Solve the compilation problem of "c2001: line breaks in constants"
- Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
- . Net interprocess communication
- C'est un petit résumé de l'étude.
- Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
- Visio draws Tai Chi
- 8. Static file
猜你喜欢
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
How to realize automatic playback of H5 video
[detailed steps of FreeRTOS shift value for the first time]
CADD course learning (8) -- virtual screening of Compound Library
Delete subsequence < daily question >
Fedora/rehl installation semanage
ETCD数据库源码分析——etcdserver bootstrap初始化存储
11. Intranet penetration and automatic refresh
One question per day (Mathematics)
Fedora/REHL 安装 semanage
随机推荐
Lombok原理和同时使⽤@Data和@Builder 的坑
After learning classes and objects, I wrote a date class
[try to hack] John hash cracking tool
The most detailed and comprehensive update content and all functions of guitar pro 8.0
cdc 能全量拉去oracle 表嘛
Fedora/REHL 安装 semanage
电脑钉钉怎么调整声音
SharedPreferences 源码分析
MySQL reported an error datetime (0) null
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
Basic explanation of turtle module - draw curve
[HBZ sharing] how to locate slow queries in cloud database
[FreeRTOS interrupt experiment]
Recommendation | recommendation of 9 psychotherapy books
Yyds dry goods inventory OSI & tcp/ip
程序员在互联网行业的地位 | 每日趣闻
Introduction of several RS485 isolated communication schemes
8. Static file
Easyrecovery靠谱不收费的数据恢复电脑软件
hashlimit速率控制