当前位置:网站首页>[django learning notes - 12]: database operation
[django learning notes - 12]: database operation
2022-06-09 04:50:00 【@Meet when the flowers fall】
List of articles
Database operation ( add to )
The early stage of the work :
1、 stay settings.py File to configure the database
2、 In sub applications __init__.py File to connect to the database
3、 Create the model class and add it to the database
Add data operations
There are two ways to add a database :
The first is to use instance objects save Method add (Family For the model class ):
fam = Family( key = value , key = value , ……)
fam.save()
When you add it again, you need to add the previous id Number
fam = Family(2, value , value , ……)( If the key name is not written, the value must correspond to the position of the key name )
fam.save()
The second kind : Model class .Manager object .create ( If we do not set up Manager Object defaults to objects)
Famliy.objects.create( key = value , key = value )
When adding again, you do not need to add the previous id Number
- Input in the terminal :python manage.py shell, Get into shell Environmental Science
- Import model classes from child applications ( example ):from afir.models import Family
- Use the first method to add :



- Use create Method add


The basic query
Preparation :
1、 Create a routing file in the sub application , Distribute routes in the master route file
2、views.py Write the view function in
- Family.fam.filter(age=20) —— It means to get age by 20 Query set object for



- Family.fam.exclude(age=20) —— It means to get something other than age by 20 Query set object for

Fuzzy query
def orm_options(request):
obj = Family.fam.filter(name__contains='e') # obtain name Contains characters e The object of
obj = Family.fam.filter(name__startswith='A') # obtain name The middle first character is A The object of
obj = Family.fam.filter(name__endswith='d') # obtain name The middle package end character is d The object of
obj = Family.fam.filter(id__in=[1, 2, 3]) # obtain id by 1,2,3 The object of
# obj = Family.fam.filter(name__in='Zhangsanlisisdga') # If the query range is in string form , It is impossible to perform pattern matching of strings
print(obj)
return HttpResponse(obj)
Comparison query
def orm_options(request):
# obj = Family.fam.filter(id__gt=4) # gt, Greater than
# obj = Family.fam.filter(id__gte=4) # gte, Is greater than or equal to
# obj = Family.fam.filter(id__lt=4) # gt, Say less than
obj = Family.fam.filter(id__lte=4) # gt, Less than or equal to
print(obj)
return HttpResponse(obj)
边栏推荐
- Typescript learning [5] type
- 2022年危险化学品经营单位主要负责人操作证考试题库及在线模拟考试
- (5) Bidirectional data binding
- (7) Attribute binding
- Clcnet: Rethink integrated modeling with classified confidence network (with source code download)
- [006] [ESP32开发笔记] 使用Flash下载工具烧录固件步骤
- Smart curly bracket escape
- Typescript learning [8] enumeration type
- View local public IP
- "Diwen Cup" skill competition between teachers and students of Electrical Engineering Department of Zibo technician college was successfully held
猜你喜欢

openGL_04使用索引数组进行绘制

openGL_01-創建窗口

Wuqi_ New progress in vision language navigation: pre training and sim2real
![[004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF](/img/55/9eb286bc56ec991837fc014b42fc20.png)
[004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF

Proteus simulation Arduino

(7) Attribute binding

Devon 2K high resolution smart screen releases 4 new products

Nacos1.1.4版本本地源码启动

keepalived配置虚拟IP

Openstack Learning Series 12: installing CEPH and docking openstack
随机推荐
Ultimate shell - Zsh
zsh
Personalized brain connectome fingerprints: their importance in cognition
Transformer裏面的緩存機制
数据库的三大范式
2022 safety officer-a certificate examination questions and online simulation examination
P1743 Audiophobia
在渗透测试中快速检测常见中间件、组件的高危漏洞
2022年高压电工考试模拟100题及答案
Recommend this UI automation testing framework and write use cases as colloquially as possible
2022-06-清华管理学-清华大学-宁向东
openGL_01-创建窗口
OpenGL 01 - créer une fenêtre
PDQ environment variables
View local public IP
Mmdet modify the font size, position, color and fill box of the detection box
[006] [ESP32開發筆記] 使用Flash下載工具燒錄固件步驟
(3) Data binding instructions
Faster RCNN
Built in objects for typescript