Guest User

Untitled

a guest
Oct 4th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. class RatingCount(APIView):
  2.     object = None
  3.  
  4.     def post(self, request):  # Getting rating count for Post or Comment object
  5.         content_type = request.data.get("content_type")
  6.         object_id = request.data.get("object_id")
  7.         if content_type == "post":
  8.             self.object = Post.objects.get(id=object_id)
  9.         elif content_type == "comment":
  10.             self.object = Comment.objects.get(id=object_id)
  11.  
  12.         rating_count = LikeDislike.objects.filter(
  13.             content_type=ContentType.objects.get_for_model(self.object), object_id=self.object.id).aggregate(
  14.             Sum('vote')).get('vote__sum') or 0
  15.         # serializer = self.serializer_class(self.object, many=True)
  16.         return Response({"rating_count": rating_count})
Advertisement
Add Comment
Please, Sign In to add comment