def post(self, request): data = request.data serializer = PostSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def put(self, request, id): data = request.data instance = self.get_object(id) serializer = PostSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200) return Response(serializer.errors, status=400)
def post_list(request, slug): posts = Post.objects.filter( blog__slug=slug ) #look up posts by the FK Publication slug. Double underscore is doing a join. serializer = PostSerializer(posts, many=True) #pass the data to the serializer return Response(serializer.data) #send back the response
def get(self, request, post_id): res = Post.objects.get(id=15) response = PostSerializer(res) return Response(data={ "data": response.data, "success": True }, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, post_id): post = Post.objects.get(id=post_id) post.title = request.data['title'] post.save() response = PostSerializer(post) return Response(data={ "data": response.data, "success": True }, status=status.HTTP_200_OK)
def get(self, request): # p = Post() res = Post.objects.all() response = PostSerializer(res, many=True) return Response(data={ "data": response.data, "success": True }, status=status.HTTP_200_OK)
def posts(request): if request.method == 'GET': posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == "POST": json_parser = JSONParser() data = json_parser.parse(request) serializer = PostSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def post(self, request): post_data = {} if 'author' not in request.data: return Response(data={ "error": "Author name must be provided", "success": False }, status=status.HTTP_400_BAD_REQUEST) p = Post() p.author = request.data['author'] p.text = request.data['text'] p.title = request.data['title'] response = p.publish(request.data) response = PostSerializer(response) print response.data return Response(data={ "data": response.data, "success": True }, status=status.HTTP_200_OK)
def post_detail2(request, id): try: instance = Post.objects.get(id=id) except Post.DoesNotExist as e: return JsonResponse({'error': "given post object does not found"}, status=404) if request.method == 'GET': serializer = PostSerializer(instance) return JsonResponse(serializer.data) elif request.method == "PUT": json_parser = JSONParser() data = json_parser.parse(request) serializer = PostSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=200) return JsonResponse(serializer.errors, status=400) elif request.method == "DELETE": instance.delete() return HttpResponse(status=204)
def index(request): post_list = Post.objects.all().order_by('-created_time') serializer = PostSerializer(post_list, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, id=None): instance = self.get_object(id) serializer = PostSerializer(instance) return Response(serializer.data)
def get(self, request): posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return Response(serializer.data, status=200)
def choices3(self, request): post = Post.objects.all() serializer = PostSerializer(post, many=True) return Response(serializer.data, status=200)
def choices2(self, request, id=None): post = self.get_object() user = User.objects.get(id=post.author.id) posts = user.post_set.all() serializer = PostSerializer(posts, many=True) return Response(serializer.data, status=200)
def choices(self, request, id=None): post = self.get_object() user_posts = Post.objects.filter(author__id=post.author.id) serializer = PostSerializer(user_posts, many=True) return Response(serializer.data, status=200)