当前位置:网站首页>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)
边栏推荐
- JS reverse tutorial second issue - Ape anthropology first question
- 【无标题】
- 第一讲:寻找矩阵的极小值
- 使用BigDecimal的坑
- 内存==c语言1
- How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
- 2016 CCPC Hangzhou Onsite
- Scratch crawler mysql, Django, etc
- Difference between process and thread
- CodeForces - 1324D Pair of Topics(二分或双指针)
猜你喜欢
AI从感知走向智能认知
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
Deep understanding of UDP, TCP
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
JS reverse tutorial second issue - Ape anthropology first question
【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
使用BigDecimal的坑
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
第一讲:寻找矩阵的极小值
AI moves from perception to intelligent cognition
随机推荐
Arthas simple instructions
Lesson 1: finding the minimum of a matrix
14th test
小程序滑动、点击切换简洁UI
PostgreSQL创建触发器的时候报错,
剑指 Offer II 107. 矩阵中的距离
Sword finger offer II 107 Distance in matrix
H5 web player easyplayer How does JS realize live video real-time recording?
Applet popup half angle mask layer
Guys, have you ever encountered the case of losing data when Flink CDC reads mysqlbinlog? Every time the task restarts, there is a probability of losing data
Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
js逆向教程第二发-猿人学第一题
字节跳动 Kitex 在森马电商场景的落地实践
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
First issue of JS reverse tutorial
Impression notes finally support the default markdown preview mode
[untitled]
CDZSC_ 2022 winter vacation personal training match level 21 (2)
位操作==c语言2