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
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
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)
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)
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)
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)
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