Beispiel #1
0
 def post(self, request, *args, **kwargs):
     # 反序列化
     # print(request.body)
     # print(request.POST)  # Django
     # print(request.data)  # DRF
     data = request.data
     s = PublisherSerializer(data=data)
     if s.is_valid():
         s.save()
         return Response("提交数据成功.")
     else:
         return Response(s.error_messages)
Beispiel #2
0
 def put(self, request, *args, **kwargs):
     data = request.data
     pk = kwargs.get('pk', None)
     if not pk:
         return Response("pk is required.")
     instance = Publisher.objects.get(pk=pk)
     s = PublisherSerializer(instance, data=data)
     if s.is_valid():
         s.save()
         return Response(s.data)
     else:
         return Response(s.errors)
Beispiel #3
0
 def get(self, request, *args, **kwargs):
     # 序列化
     # print(request.GET)  # Django
     # print(request.query_params) # DRF
     ps = Publisher.objects.all()
     s = PublisherSerializer(ps, many=True)
     return Response(s.data)
Beispiel #4
0
    def get(self, request, *args, **kwargs):
        # 序列化
        # for k, v in request.META.items():
        #     print(k, v)
        # print(request.META['CONTENT_TYPE'])
        # print(request.GET)  # Django
        # print(request.query_params) # DRF
        print(args)
        # http://localhost:8000/api/v2/publish_v2/14/
        print(kwargs)  # {'pk' : 14}
        pk = kwargs.get('pk', None)
        if pk:
            # instance = Publisher.objects.get(pk=pk)\
            try:
                instance = Publisher.objects.get(pk=pk)
            except Publisher.DoesNotExist as e:
                return Response("{}记录不存在".format(pk))

            s = PublisherSerializer(instance)
        else:
            instances = Publisher.objects.all()
            s = PublisherSerializer(instances, many=True)
        return Response(s.data)