当前位置:网站首页>ORM -- database addition, deletion, modification and query operation logic
ORM -- database addition, deletion, modification and query operation logic
2022-07-07 10:07:00 【chuntian_ tester】
1. Data acquisition logic
1.1 Get multiple pieces of data


1.2 Get a single piece of data



2. Create data logic


3. Modify the data logic
3.1 Data before modification

3.2 modify


3.3 After modification

4. Delete data logic
When deleting data from a table , From the table model, if you define foreign keys on_delete=models.CASCADE attribute , Then from the association in the table
Data will also be deleted .


The above example code :
# /projects/views.py
import json
from django.http import HttpResponse, JsonResponse
# Create your views here.
# Subapplication view
from django.views import View
from projects import models
class ProjectsView(View):
"""
Class view
:param View: Inherited from from django.views import View
:return: HttpResponse object
"""
def get(self, request):
# Query all project information
# 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):
# New projects
# POST /projects/ json Format parameters
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': ' Create success '
}
return JsonResponse(result, safe=False, status=203)
class ProjectDetailView(View):
def get(self, request, pk):
# Query the specified item
# 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):
# Modification item
# PUT /projects/<int:pk>/ json Format parameters
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': ' The update is successful ',
'count': count,
}
return JsonResponse(result, status=203)
def delete(self, request, pk):
# Delete the project
# DELETE /projects/<int:pk>
status = models.Projects.objects.filter(id=pk).delete()
result = {
'msg': f' Delete {pk} success ',
'count': status,
}
return JsonResponse(result, safe=False, status=203)
边栏推荐
- Deadlock caused by non clustered index in SQL Server
- Main (argc, *argv[]) details
- 字节跳动 Kitex 在森马电商场景的落地实践
- China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
- ORM--查询类型,关联查询
- ORM模型--关联字段,抽象模型类
- 有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
- Application of C # XML
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- 能源路由器入门必读:面向能源互联网的架构和功能
猜你喜欢
随机推荐
使用BigDecimal的坑
Luogu p2482 [sdoi2010] zhuguosha
2020CCPC威海 J - Steins;Game (sg函数、线性基)
Pytest learning - dayone
C socke server, client, UDP
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Deep understanding of UDP, TCP
Bit operation ==c language 2
哈夫曼编码压缩文件
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
ORM model -- associated fields, abstract model classes
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
Introduction to uboot
Arcgis操作: 批量修改属性表
小程序滑动、点击切换简洁UI
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
Memory ==c language 1
Advanced function learning in ES6
flink. CDC sqlserver. You can write the DEM without connector in sqlserver again







