Example #1
0
 def post(self, req):
     params = json.loads(req.body.decode())
     data = {
         'name': params.get('name'),
         'description': params.get('description'),
         'book': params.get('book')
     }
     serializer = PeopleSerializer(data=data)
     if serializer.is_valid(raise_exception=True):
         serializer.save()
         return HttpResponse('http validate ok')
     else:
         return Response(status=201)
Example #2
0
 def get(self, req, id):
     try:
         people = PeopleInfo.objects.get(pk=id)
     except PeopleInfo.DoesNotExist:
         return HttpResponse(status=400)
     serializer = PeopleSerializer(instance=people)
     return Response(serializer.data)
Example #3
0
    def put(self, req, id):
        try:
            people = PeopleInfo.objects.get(pk=id)
        except PeopleInfo.DoesNotExist:
            return HttpResponse(status=400)
        params = json.loads(req.body.decode())
        # TODO: update 时 data 怎么写, 老师用的 manager shell 自己写的 data 类
        data = {
            'name': params.get('name'),
            'description': params.get('description'),
            'book': params.get('book')
        }

        serializer = PeopleSerializer(instance=people, data=data)
        if serializer.is_valid(raise_exception=True):
            # Restful serializer 如何返回数据
            data = serializer.save()
            print(data)
            return JsonResponse(data=serializer.data)
        else:
            return Response(serializer.data, status=202)
Example #4
0
 def get(self, req):
     peoples = PeopleInfo.objects.all()
     serializer = PeopleSerializer(instance=peoples, many=True)
     return Response(data=serializer.data)