Ejemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        if pk:
            article = get_object_or_404(Article, pk=pk)
            serializer = ArticleSerializer(article)
        else:
            articles = Article.objects.all()
            serializer = ArticleSerializer(articles, many=True)
        response_data = serializer.data

        return Response(data=response_data)
Ejemplo n.º 2
0
 def put(self, request, pk):
     article = get_object_or_404(Article.objects.all(), pk=pk)
     data = json.loads(request.body)
     slr = ArticleSerializer(instance=article, data=data, partial=True)
     if slr.is_valid(raise_exception=True):
         article = slr.save()
         return Response(slr.data)
     else:
         response = JsonResponse(slr.errors, safe=False)
         response.status_code = 400
         return response
Ejemplo n.º 3
0
 def get(self, request, *args, **kwargs):
     try:
         article = Article.objects.get(pk=kwargs.get("pk"))
         article_srlz = ArticleSerializer(article)
         response_data = article_srlz.data
         return Response(response_data)
     except Article.DoesNotExist:
         return Response(data={"error": "error"}, status=404)
Ejemplo n.º 4
0
 def put(self, request, pk, *args, **kwargs):
     article = get_object_or_404(Article, pk=pk)
     article_data = request.data
     serializer = ArticleSerializer(article, data=article_data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data)
Ejemplo n.º 5
0
 def post(self, request, *args, **kwargs):
     article_data = request.data
     serializer = ArticleSerializer(data=article_data)
     serializer.is_valid(raise_exception=True)
     article = serializer.save()
     return JsonResponse({'id': article.id})
Ejemplo n.º 6
0
    def get(self, request, *args, **kwargs):
        articles = Article.objects.all()
        serializer = ArticleSerializer(articles, many=True)
        response_data = serializer.data

        return Response(data=response_data)
 def put(self, request, pk, *args, **kwargs):
     article_upd = get_object_or_404(Article, pk=pk)
     serializer = ArticleSerializer(article_upd, data=request.data)
     serializer.is_valid(raise_exception=True)
     article_s = serializer.save()
     return Response({'id': article_s.pk})
 def get(self, request, pk, *args, **kwargs):
     context = {}
     article = Article.objects.filter(pk=pk)
     serializer = ArticleSerializer(article, many=True).data
     context['data_article'] = serializer
     return Response(context, status=200)