Ver código fonte

7-22作业第二步:覆盖job岗位的list逻辑

DYaiu 4 anos atrás
pai
commit
16973f684c
1 arquivos alterados com 18 adições e 1 exclusões
  1. 18 1
      treeAndTable/views.py

+ 18 - 1
treeAndTable/views.py

@@ -1,4 +1,5 @@
 from django.db.models import F
+from django.forms import model_to_dict
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
@@ -51,6 +52,7 @@ class DepartmentView(APIView):
             return Response(data={}, status=status.HTTP_400_BAD_REQUEST)
         return Response(data=data, status=status.HTTP_200_OK)
 
+
 '''
 post
 {
@@ -67,6 +69,8 @@ Put
     "job":6
 }
 '''
+
+
 class DeptAndJobView(ModelViewSet):
     queryset = Dept_job.objects.all()
     serializer_class = Dept_Job_Serializer
@@ -86,7 +90,7 @@ class DeptAndJobView(ModelViewSet):
             deptId = request.data['dept_id']
             name = request.data['name']
         except Exception:
-            return Response(data={'msg':'请求参数错误'}, status=status.HTTP_400_BAD_REQUEST)
+            return Response(data={'msg': '请求参数错误'}, status=status.HTTP_400_BAD_REQUEST)
         try:
             # 如果给定的岗位名称不存在岗位表直接新建一个岗位
             job = Job.objects.get(name=name)
@@ -96,3 +100,16 @@ class DeptAndJobView(ModelViewSet):
         Dept_job.objects.create(dept_id=deptId, job_id=job.id)
 
         return Response(data={'msg': 'success'}, status=status.HTTP_200_OK)
+
+    '''
+    根据部门id获取部门岗位中间表的该部门所有岗位信息
+    '''
+
+    def list(self, request, *args, **kwargs):
+        deptId = request.GET.get('dept_id')
+        print(deptId)
+        deptAndJob = Dept_job.objects.filter(dept_id=deptId).all()
+        data = []
+        for DJ in deptAndJob:
+            data.append(model_to_dict(DJ.job))
+        return Response(data=data, status=status.HTTP_200_OK)