当前位置:网站首页>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)
边栏推荐
- How to use clipboard JS library implements copy and cut function
- Detailed explanation of diffusion model
- flink. CDC sqlserver. You can write the DEM without connector in sqlserver again
- AI从感知走向智能认知
- 哈夫曼编码压缩文件
- [4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision
- PostgreSQL reports an error when creating a trigger,
- Liunx command
- 高斯消元
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
猜你喜欢
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
# Arthas 简单使用说明
Delete a record in the table in pl/sql by mistake, and the recovery method
Win10安装VS2015
[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development
Lesson 1: finding the minimum of a matrix
Switching value signal anti shake FB of PLC signal processing series
企业实战|复杂业务关系下的银行业运维指标体系建设
JS逆向教程第一发
Sqlplus garbled code problem, find the solution
随机推荐
一大波开源小抄来袭
Esp8266 uses TF card and reads and writes data (based on Arduino)
Selenium+bs4 parsing +mysql capturing BiliBili Tarot data
Why are social portals rarely provided in real estate o2o applications?
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
How to solve the problem of golang select mechanism and timeout
Delete a record in the table in pl/sql by mistake, and the recovery method
Lesson 1: hardness of eggs
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
Natapp intranet penetration
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Sword finger offer II 107 Distance in matrix
Bean 作⽤域和⽣命周期
In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen
基础篇:带你从头到尾玩转注解
Garbage disposal method based on the separation of smart city and storage and living digital home mode
第一讲:寻找矩阵的极小值
PostgreSQL创建触发器的时候报错,
Three years after graduation