当前位置:网站首页>Django learning - model and database operation (II)
Django learning - model and database operation (II)
2022-06-22 03:59:00 【Today Zhang】
1、mysql as well as mysql-workbench install (MAC-OS)
- mysql download
- mysql-workbench download
2、Django Database operation
- To configure Django To link a database mysql

- Configure Links mysql Database information

- Use mysql-bench newly build Database name by :byte

- stay testdj In the project newly build app(TestModels), As shown in the figure :

- newly build app The order is as follows :
django-admin startapp TestModel
- stay setting.py in To configure TestModels, This can be done in testdj Direct drinking under the directory testModel Module content

- stay TestModel/models.py Write the database model in

- stay terminal Execute the following commands in :
$ python manage.py migrate # Create a table structure
$ python manage.py makemigrations TestModel # Give Way Django Know we have some changes in our model
$ python manage.py migrate TestModel # Create a table structure
- If successful , stay mysql As shown in the following figure

- Add operation

# 1、 Add operation
test1 = Test(name='byte')
test1.save()
# return HttpResponse("<p> Data added successfully !</p>")
- Query operation

# 2、 Query operation
response = ""
response1 = ""
# adopt objects Of the model manager all() Get all the data lines , amount to SQL Medium SELECT * FROM
list = Test.objects.all()
# filter amount to SQL Medium WHERE, Conditions can be set to filter results
response2 = Test.objects.filter(id=5)
# Get single object
response3 = Test.objects.get(id=5)
# Limit data returned amount to SQL Medium OFFSET 0 LIMIT 2;
list1 = Test.objects.order_by('name')[0:2]
# Data sorting
# Test.objects.order_by("id")
# # The above method can be used in chain
# Test.objects.filter(name="byte").order_by("id")
# Output all data
for var in list1:
response1 += var.name + " "
response = response1
- update operation

# # 3. Update modification operation
# test1 = Test.objects.get(id=1)
# test1.name = 'byte-Google-1'
# test1.save()
# Another way
# Test.objects.filter(id=1).update(name='Google')
# Modify all columns
# Test.objects.all().update(name='Google')
- Delete operation

# 4. Delete operation
# Delete id=1 The data of
# test1 = Test.objects.pop(id)
# test1.delete()
# Another way
# Test.objects.filter(id=1).delete()
# Delete all data
# Test.objects.all().delete()
边栏推荐
- How does twitter decentralize? Look at these ten socialfi projects
- Docker install redis
- 快速排序
- WPF achieves star effect
- 【BP回归预测】基于matlab GA优化BP回归预测(含优化前的对比)【含Matlab源码 1901期】
- Huffman tree
- Relationship among original code, complement code and inverse code
- 物联网UWB技术方案,智能UWB精准定位,厘米级定位精度
- CAPL学习之路-以太网函数
- Larave 数据库备份 定时任务
猜你喜欢

图的DFS
![[leetcode] 17 backtracking (letter combination of telephone number)](/img/86/e623383ded25677d37bd1c7e98a772.png)
[leetcode] 17 backtracking (letter combination of telephone number)

Kubernetes cluster log management

Dynamic planning - Taking stair climbing with minimum cost as an example

Larave 数据库备份 定时任务

Shutter status management

试用了多款报表工具,终于找到了基于.Net 6开发的一个了

Flutter-状态管理

Beifu twincat3 third-party servo motor -- Taking Huichuan is620n servo as an example

Online text batch inversion by line tool
随机推荐
How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
Kubernetes cluster log management
Introduction to various models of Beifu embedded controller PLC
Larave 数据库备份 定时任务
Read stream special attention
Existing requirements synchronize other database user information to our system. Their primary key ID is string and our primary key is long
The following assertion was thrown during performlayout
Beifu TwinCAT 3 cylinder action program compilation
It is about one-step creating Yum source cache in Linux
Flutter-渲染原理&三棵树详解
A solution to the conflict between the keywords of DM data and the fields of the table
Flutter-状态管理
Flutter 颜色渐变及模仿淘宝渐变关注按钮
128 traps - source code analysis
Key program of TwinCAT 3 RS232 communication
LeetCode --- 1221. Split a string in balanced strings problem solving Report
[day 23] given an array of length N, return the array after deleting the x-th element | array deletion operation
线索二叉树
The "two lives" I spent in Huawei (learning from those who performed well in large factories)
Basic concept of graph