def get(self, request, username=None): """ 获取员工信息 """ if username is None: serializer = EmpSerializer(Emp.objects.all(), many=True) return JsonResponse(serializer.data, safe=False) else: emp = get_object_or_404(Emp, username=username) serializer = EmpSerializer(emp) return JsonResponse(serializer.data, safe=False)
def emp_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': emps = Emp.objects.all() serializer = EmpSerializer(emps, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = EmpSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) else: return JSONResponse(serializer.errors, status=400)
def post(self, request): """ 增加员工信息 """ data = JSONParser().parse(request) serializer = EmpSerializer(data=data) if not serializer.is_valid(): raise ParseError(serializer.errors) serializer.save() return JsonResponse(serializer.data, safe=False)
def put(self, request, username): """ 修改员工信息 """ data = JSONParser().parse(request) emp = get_object_or_404(Emp, username=username) serializer = EmpSerializer(emp, data=data, partial=True) if not serializer.is_valid(): raise ParseError(serializer.errors) serializer.save() return JsonResponse(serializer.data, safe=False)
def AddEmployee(request): if request.method == 'POST': serializer = EmpSerializer(data=request.POST) print "" print serializer.is_valid() print "++++++++++++++" if serializer.is_valid(): serializer.save() else: print serializer.errors return JsonResponse({'error': serializer.errors}) return JsonResponse({'data': serializer.data})
def emp_detail(request,pk): """ Retrieve, update a code snippet. """ try: emp = Emp.objects.get(pk=pk) except Emp.DoesNotExist: return HttpResponse(status=400) if request.method == 'GET': serializer = EmpSerializer(emp) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = EmpSerializer(emp, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) else: return JSONResponse(serializer.error,status=400) elif request.method == 'DELETE': emp.delete() return HttpResponse(status=204)