view代码:
import os
import shutil
from django.http import StreamingHttpResponse, FileResponse, HttpResponse
from django.shortcuts import render
# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from common.play_file_ep import upload
from .serializers import ToolSrializers, ToolTablesSrializers
from .models import *
class Tools(APIView):
# 新增
def post(self, request):
number = request.data.get("number")
id = request.data.get('id')
tag = request.data.get('tag')
describe = request.data.get('describe')
type = request.data.get('type')
name = request.data.get('name')
srializers = ToolSrializers(data={
'id': id,
'name': name,
'type': type,
})
srializers.is_valid(raise_exception=True)
srializers.save()
srializers2 = ToolTablesSrializers(data={
'number': number,
"id": id,
'tag': tag,
'describe': describe,
})
srializers2.is_valid(raise_exception=True)
srializers2.save()
return Response({'code': 200, 'message': 'ok', 'data': srializers.data})
# 下载
def get(self, request):
'''
下载接口
:param request:
:return:
'''
z_name = 'test.zip'
z_file = open(r'E:\text.zip', 'rb')
data = z_file.read()
z_file.close()
response = HttpResponse(data, content_type='application/zip')
from urllib import parse
response['Content-Disposition'] = 'attachment;filename = '+parse.quote(z_name)
return response
# 修改
def put(self, request):
number = request.data.get("number")
id = request.data.get('id')
tag = request.data.get('tag')
describe = request.data.get('describe')
type = request.data.get('type')
name = request.data.get('name')
try:
queryset = Tool.objects.filter().first()
except Tool.DoesNotExist:
return Response({'code':400, 'msg': '资源不存在'})
srializers = ToolSrializers(instance=queryset, data={
'id': id,
'name': name,
'type': type,
})
srializers.is_valid(raise_exception=True)
srializers.save()
query2 = ToolTable.objects.filter().first()
srializers2 = ToolTablesSrializers(instance=query2, data={
'number': number,
"id": id,
'tag': tag,
'describe': describe,
})
srializers2.is_valid(raise_exception=True)
srializers2.save()
return Response({'code':200, 'msg':'修改成功'})
# 删除
def delete(self, request):
id = request.data.get('id')
queryset = Tool.objects.filter(id=id).first()
queryset.delete()
return Response({'code': 200, 'msg': '删除成功'})
# 显示
class ShowTool(APIView):
def get(self, request):
data = ToolTable.objects.all()
# 资源列表
lis = []
for i in data:
lis.append({
'name': i.id.name,
'type': i.id.type,
'number': i.number,
"id": i.id_id,
'tag': i.tag,
'describe': i.describe,
})
return Response({'data': lis})
model代码:
# id表
class Tool(models.Model):
id = models.CharField(max_length=256, verbose_name='id', primary_key=True)
type = models.CharField(max_length=256, verbose_name='类型')
name = models.CharField(max_length=256, verbose_name='名称')
class Meta:
db_table = 'tool'
verbose_name = 'id表'
# 信息表
class ToolTable(models.Model):
number = models.CharField(max_length=88, verbose_name='编号')
id = models.ForeignKey(to=SimulationToolTable, on_delete=models.CASCADE,
verbose_name='id')
tag = models.CharField(max_length=256, verbose_name='版本', primary_key=True)
describe = models.TextField(verbose_name='描述')
register_time = models.DateTimeField(auto_now_add=True, verbose_name='注册日期')
class Meta:
db_table = 'tooltable'
verbose_name = '信息表'
serializers代码
from rest_framework import serializers
from .models import *
class ToolSrializers(serializers.ModelSerializer):
class Meta:
model = Tool
fields = "__all__"
class ToolTables(serializers.ModelSerializer):
class Meta:
model = ToolRegister
fields = "__all__"
admin代码
admin.site.register(Tool)
admin.site.register(ToolTable)