当前位置:网站首页>ORM--数据库增删改查操作逻辑
ORM--数据库增删改查操作逻辑
2022-07-07 07:17:00 【chuntian_tester】
1.数据获取逻辑
1.1 获取多条数据
1.2 获取单条数据
2.创建数据逻辑
3.修改数据逻辑
3.1 修改前的数据
3.2 修改
3.3 修改后
4.删除数据逻辑
删除表中数据时,从表模型若定义外键on_delete=models.CASCADE属性,则从表中的关联
数据也会被删除。
以上示例代码:
# /projects/views.py
import json
from django.http import HttpResponse, JsonResponse
# Create your views here.
# 子应用视图
from django.views import View
from projects import models
class ProjectsView(View):
"""
类视图
:param View: 继承自from django.views import View
:return: HttpResponse对象
"""
def get(self, request):
# 查询所有项目信息
# GET /projects/
projects_list = []
qs = models.Projects.objects.all()
for item in qs:
projects_list.append(
{
'id': item.id,
'name': item.full_name,
'leader': item.leader,
'create_time': item.create_time,
'update_time': item.update_time,
}
)
return JsonResponse(projects_list, safe=False)
def post(self, request):
# 新建项目
# POST /projects/ json格式参数
python_data = json.loads(request.body)
pro_obj = models.Projects.objects.create(full_name=python_data['name'], leader=python_data['leader'],
is_execute=python_data['is_execute'], desc=python_data['desc'])
result = {
'full_name': pro_obj.full_name,
'leader': pro_obj.leader,
'desc': pro_obj.desc,
'msg': '创建成功'
}
return JsonResponse(result, safe=False, status=203)
class ProjectDetailView(View):
def get(self, request, pk):
# 查询指定项目
# GET /projects/<int:pk>/
pro_obj = models.Projects.objects.get(id=pk)
result = {
'id': pro_obj.id,
'name': pro_obj.full_name,
'leader': pro_obj.leader,
'create_time': pro_obj.create_time,
'update_time': pro_obj.update_time,
}
return JsonResponse(result, status=203)
def put(self, request, pk):
# 修改项目
# PUT /projects/<int:pk>/ json格式参数
python_data = json.loads(request.body)
count = models.Projects.objects.filter(id=pk).update(full_name=python_data['name'],
leader=python_data['leader'])
result = {
'data': python_data,
'msg': '更新成功',
'count': count,
}
return JsonResponse(result, status=203)
def delete(self, request, pk):
# 删除项目
# DELETE /projects/<int:pk>
status = models.Projects.objects.filter(id=pk).delete()
result = {
'msg': f'删除{pk}成功',
'count': status,
}
return JsonResponse(result, safe=False, status=203)
边栏推荐
- Dynamics 365online applicationuser creation method change
- [untitled]
- flink. CDC sqlserver. You can write the DEM without connector in sqlserver again
- How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
- Hcip first day notes sorting
- Codeforces - 1324d pair of topics
- CDZSC_2022寒假个人训练赛21级(1)
- 内存==c语言1
- 中国首款电音音频类“山野电音”数藏发售来了!
- The industrial chain of consumer Internet is actually very short. It only undertakes the role of docking and matchmaking between upstream and downstream platforms
猜你喜欢
JS reverse tutorial second issue - Ape anthropology first question
Lecture 1: stack containing min function
Sqlplus garbled code problem, find the solution
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
Use 3 in data modeling σ Eliminate outliers for data cleaning
第一讲:寻找矩阵的极小值
Switching value signal anti shake FB of PLC signal processing series
Esp8266 uses TF card and reads and writes data (based on Arduino)
Elaborate on MySQL mvcc multi version control
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
随机推荐
Bean 作⽤域和⽣命周期
How to use clipboard JS library implements copy and cut function
Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
基于智慧城市与储住分离数字家居模式垃圾处理方法
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
Diffusion模型详解
Flinkcdc failed to collect Oracle in the snapshot stage. How do you adjust this?
第一讲:寻找矩阵的极小值
【无标题】
Win10安装VS2015
C socke server, client, UDP
Flex flexible layout
Huffman encoded compressed file
Oracle installation enhancements error
C# Socke 服务器,客户端,UDP
Pit using BigDecimal
[untitled]
La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
The combination of over clause and aggregate function in SQL Server
Addition, deletion, modification and query of ThinkPHP database