当前位置:网站首页>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)
边栏推荐
- 2020浙江省赛
- 官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
- Es classes and objects, prototypes
- [untitled]
- 20排位赛3
- Google Colab装载Google Drive(Google Colab中使用Google Drive)
- 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
- C# XML的应用
- ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
- How to use Mongo shake to realize bidirectional synchronization of mongodb in shake database?
猜你喜欢
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
Agile course training
[untitled]
Memory ==c language 1
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Qualifying 3
arcgis操作:dwg数据转为shp数据
能源路由器入门必读:面向能源互联网的架构和功能
Flex flexible layout
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
随机推荐
There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
CDZSC_ 2022 winter vacation personal training match level 21 (1)
Use 3 in data modeling σ Eliminate outliers for data cleaning
uboot机构简介
Luogu p2482 [sdoi2010] zhuguosha
小程序滑动、点击切换简洁UI
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
phpcms实现PC网站接入微信Native支付
小程序实现页面多级来回切换支持滑动和点击操作
Applet sliding, clicking and switching simple UI
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
使用BigDecimal的坑
位操作==c语言2
VS Code指定扩展安装位置
Internship log - day04
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
Internship log - day07
Codeforces - 1324d pair of topics
20排位赛3
conda离线创建虚拟环境