Django的APIView增删改查

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值