当前位置:网站首页>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)
边栏推荐
猜你喜欢

In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen

First issue of JS reverse tutorial

Oracle installation enhancements error

How to use clipboard JS library implements copy and cut function

Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat

使用BigDecimal的坑

The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations

Basic chapter: take you through notes

CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)

印象笔记终于支持默认markdown预览模式
随机推荐
剑指 Offer II 107. 矩阵中的距离
【无标题】
AI moves from perception to intelligent cognition
ViewPager2和VIewPager的区别以及ViewPager2实现轮播图
Huffman encoded compressed file
C# XML的应用
Esp8266 uses TF card and reads and writes data (based on Arduino)
Performance optimization record of the company's product "yunzhujia"
Guys, how can mysql-cdc convert the upsert message to append only
14th test
JS reverse tutorial second issue - Ape anthropology first question
Lecture 1: stack containing min function
Liunx command
Switching value signal anti shake FB of PLC signal processing series
CDZSC_ 2022 winter vacation personal training match level 21 (2)
Writing file types generated by C language
PostgreSQL创建触发器的时候报错,
**Grafana installation**
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
[4G/5G/6G专题基础-146]: 6G总体愿景与潜在关键技术白皮书解读-1-总体愿景