How to get the total number of loved clicked? My attempt is below, but it is not working.
This is my model:
class Review(models.Model):
reviewer = models.ForeignKey(User, related_name='review', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='review', on_delete=models.CASCADE, null=True)
date_time = models.DateTimeField(auto_now_add=True)
comment = models.TextField(max_length=250, blank=True, null=True)
rating = models.CharField(choices=RATING_CHOICES, max_length=150)
loved = models.BooleanField(default=False)
This is my view:
class ListReviewAPIView(generics.ListAPIView):
queryset = Review.objects.filter(loved="True").count()
serializer_class = ReviewSerializers
The serializers
class ReviewSerializers(serializers.ModelSerializer):
class Meta:
model = Review
fields = ['product', 'comment', 'rating', 'loved', 'date_time', 'reviewer']
Can't you just override get method to simplify what you want to do?
class GetReviewRate(APIView):
def get(self, request, *args, **kwargs):
count = Review.objects.filter(loved=True).count()
return Response({'loved_count': count})
Also Boolean values are False/True
, not "False"/"True"
, they're not strings. Remember about that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.