示例#1
0
 def create(self, request):
     slr = ArticleSerializer(data=request.data, context={'request': request})
     if slr.is_valid():
         article = slr.save()
         return Response(slr.data)
     else:
         return Response(slr.errors, status=400)
示例#2
0
 def post(self, request, *args, **kwargs):
     slr = ArticleSerializer(data=request.data)
     if slr.is_valid():
         article = slr.save()
         return Response(slr.data)
     else:
         return Response(slr.errors, status=400)
示例#3
0
 def update(self, request, pk=None):
     article = get_object_or_404(Article, pk=pk)
     slr = ArticleSerializer(data=request.data, instance=article, context={'request': request})
     if slr.is_valid():
         article = slr.save()
         return Response(slr.data)
     else:
         return Response(slr.errors, status=400)
示例#4
0
 def put(self, request, *args, **kwargs):
     article = get_object_or_404(Article, pk=kwargs.get('pk'))
     slr = ArticleSerializer(data=request.data)
     if slr.is_valid():
         article =slr.update(article, slr.validated_data)
         return Response(slr.data)
     else:
         return Response(slr.errors, status=400)
示例#5
0
    def put(self, request, *args, **kwargs):
        saved_article = get_object_or_404(Article.objects.all(), pk=pk)
        data = request.data.get('article')
        slr = ArticleSerializer(instance=saved_article, data=data, partial=True)

        if slr.is_valid(raise_exception=True):
            article_saved = slr.save()

        return JsonResponse(slr.data, safe=False)
示例#6
0
 def post(self, request, *args, **kwargs):
     data = json.loads(request.body)
     slr = ArticleSerializer(data=data)
     if slr.is_valid():
         slr.save()
         return JsonResponse(slr.data, safe=False)
     else:
         response = JsonResponse(slr.errors, safe=False)
         response.status_code = 400
         return response
示例#7
0
 def get(self, request, *args, **kwargs):
     article = Article.objects.get(pk=kwargs['pk'])
     slr = ArticleSerializer(article=article)
     if slr.is_valid():
         article = slr.save()
         return JsonResponse(slr.data, safe=False)
     else:
         response = JsonResponse(slr.errors, safe=False)
         response.status_code = 400
         return response
示例#8
0
 def post(self, request, *args, **kwargs):
     data = json.loads(request.body)
     object = Article.objects.get(pk=self.kwargs['pk'])
     slr = ArticleSerializer(data=data, instance=object)
     if slr.is_valid():
         article = slr.update()
         return JsonResponse(slr.data, safe=False)
     else:
         response = JsonResponse(slr.errors, safe=False)
         response.status_code = 400
         return response
示例#9
0
 def put(self, request, *args, **kwargs):
     object = get_object_or_404(Article, pk=kwargs.get('pk'))
     data = json.loads(request.body)
     slr = ArticleSerializer(data=data, instance=object)
     if slr.is_valid():
         slr.save()
         return JsonResponse(slr.data, safe=False)
     else:
         response = JsonResponse(slr.errors, safe=False)
         response.status_code = 400
         return response
示例#10
0
 def get(self, request, *args, **kwargs):
     data = Article.objects.get(pk=kwargs['pk'])
     slr = ArticleSerializer(data=data)
     pk = slr.data['id']
     if slr.is_valid():
         data.delete()
         return HttpResponse(pk)
     else:
         response = JsonResponse(slr.errors, safe=False)
         response.status_code = 400
         return response