(十)Django 创建接口-v1.0

2019-10-16  本文已影响0人  足__迹

新增项目

image.png

数据处理的过程

image.png

举例

class ProjectsView(View):

    def get(self, request):
        # 获取数据库信息
        project_qs = Projects.objects.all()
        # 将模型类是实例化转化为字典(嵌套字典的类表)
        project_list = []
        for project in project_qs:
            # one_dict = {
            # 'name': project.name,
            # 'leader': project.leader,
            # 'tester': project.tester,
            # 'programer': project.programer,
            # 'publish_app': project.publish_app,
            # 'desc': project.desc,
            # }
            # project_list.append(one_dict)
            project_list.append({
                'name': project.name,
                'leader': project.leader,
                'tester': project.tester,
                'programer': project.programer,
                'publish_app': project.publish_app,
                'desc': project.desc,

            })
        # jsonRespose 第一个参数只能为dict字典,如果返回其他类型,需要safe=False
        return JsonResponse(data=project_list, safe=False, status=200)

    def post(self, request):
        '''
        新增项目
        :param request:  传入的参数
        :return:
        '''
        # 获取前端传入的参数,转换为Python中的类型
        # 为了严谨需要做校验,例如是否为json

        # 1.
        json_data = request.body.decode('utf-8')
        python_data = json.loads(json_data, encoding='utf-8')
        # 2.向项目中添加数据
        # new_projiet = Projects.objects.create(name=python_data['name'],leader=python_data['leader'],tester=python_data['tester'],
        #                         property=python_data['property'],publish_app=python_data['publish_app'],
        #                         desc=python_data['desc'])
        # 可简写为
        project = Projects.objects.create(**python_data)
        # 返回数据 将模型类对象转换为字典
        one_dict = {
            'name': project.name,
            'leader': project.leader,
            'tester': project.tester,
            'programer': project.programer,
            'publish_app': project.publish_app,
            'desc': project.desc,
        }

        return JsonResponse(data=one_dict,safe=False,status=201)

class ProjrctView2(View):

    def get(self,request,pk):
        #获取前端传递的Pk值,类型是否为正整数,数据库中是否存在等
        #获取指定pk的项目
        project = Projects.objects.get(id =pk)
        #项模型对象转为字典
        one_dict = {
            'name': project.name,
            'leader': project.leader,
            'tester': project.tester,
            'programer': project.programer,
            'publish_app': project.publish_app,
            'desc': project.desc,
        }
        return JsonResponse(data=one_dict, safe=False, status=201)


    def put(self,request,pk):
        #1,获取前端传入的PK值信息
        #2, 获取指定的指定的项目
        project = Projects.objects.get(id=pk)
        # 3,获取前端传入的修改信息
        json_data = request.body.decode('utf-8')
        python_data = json.loads(json_data, encoding='utf-8')
        # 4,更新项目
        project.name = python_data['name']
        project.leader = python_data['leader']
        project.tester = python_data['tester']
        project.programer = python_data['programer']
        project.publish_app = python_data['publish_app']
        project.desc = python_data['desc']
        project.save()

        # 5,项模型类转化为字典
        one_dict = {
            'name': project.name,
            'leader': project.leader,
            'tester': project.tester,
            'programer': project.programer,
            'publish_app': project.publish_app,
            'desc': project.desc,
        }

        return JsonResponse(data=one_dict, safe=False, status=201)

    def delete(self,request,pk):
        project = Projects.objects.get(id=pk)

        project.delete()
        return JsonResponse(data=None, safe=False, status=204)

上一篇下一篇

猜你喜欢

热点阅读