当前位置:网站首页>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)
边栏推荐
- 有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
- 喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
- Apprentissage avancé des fonctions en es6
- Using keras in tensorflow to build convolutional neural network
- The new activity of "the arrival of twelve constellations and goddesses" was launched
- Switching value signal anti shake FB of PLC signal processing series
- ORM model -- associated fields, abstract model classes
- ORM--分组查询,聚合查询,查询集QuerySet对象特性
- Web3.0 series distributed storage IPFs
- 高数_第1章空间解析几何与向量代数_向量的数量积
猜你喜欢

XML配置文件解析与建模

小程序滑动、点击切换简洁UI

ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

Introduction to energy Router: Architecture and functions for energy Internet

The landing practice of ByteDance kitex in SEMA e-commerce scene

ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作

ES类和对象、原型

【原创】程序员团队管理的核心是什么?

Performance optimization record of the company's product "yunzhujia"

China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
随机推荐
Gym - 102219j kitchen plates (violent or topological sequence)
CDZSC_2022寒假个人训练赛21级(2)
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
Scratch crawler mysql, Django, etc
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
能源路由器入门必读:面向能源互联网的架构和功能
CDZSC_2022寒假个人训练赛21级(1)
C# Socke 服务器,客户端,UDP
中国首款电音音频类“山野电音”数藏发售来了!
为什么安装mysql时starting service报错?(操作系统-windows)
Horizontal split of database
Gauss elimination
2016 CCPC Hangzhou Onsite
Phpcms realizes PC website access to wechat native payment
Use 3 in data modeling σ Eliminate outliers for data cleaning
哈夫曼编码压缩文件
Sqlplus garbled code problem, find the solution
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
虚数j的物理意义