K
K
kieko2020-11-23 20:51:53
Django
kieko, 2020-11-23 20:51:53

TypeError: 'QuerySet' object is not callable??

total_comments = Comment.objects.filter(movie_id=object_id)().count() TypeError
: 'QuerySet' object is not callable


class MoviesTop(APIView):

    def get(self, request):

        if 'start_date' in request.data and 'end_date' in request.data:
            start_timestamp = request.data['start_date']
            end_timestamp = request.data['end_date']
            topMovie = Movie.objects.annotate(
                num_comments=Count(
                    'Comments',
                    filter=Q(Comments__pub_date__range=(
                        start_timestamp, end_timestamp))
                )
                ).order_by('-num_comments')
            serializer = MoviesTopFrameSerializator(topMovie,context = {'start_timestamp':start_timestamp,'end_timestamp':end_timestamp},many = True)
           
        else:
            
            topMovie=Movie.objects.annotate(
                num_comments=Count('Comments')).order_by('-num_comments')
            
            serializer=MoviesTopSerializator(topMovie, many=True)
        
        return Response(serializer.data)


serializer:

class MoviesTopSerializator(serializers.ModelSerializer):

    id_movie = serializers.IntegerField(source='id')
    comment_count = serializers.SerializerMethodField('get_count_comment')
    

    class Meta:
        model = Movie
        fields = ['id_movie', 'comment_count' ]

    def get_count_comment(self, object_id):
        total_comments = Comment.objects.filter(movie_id=object_id)().count()
        return total_comments

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-11-23
@bacon

total_comments = Comment.objects.filter(movie_id=object_id)().count()
TypeError: 'QuerySet' object is not callable
you even have the problematic code in the error, and not the one you show

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question