Skip to content Skip to sidebar Skip to footer

Annotate Many To Many Relation In Django

I have two models User and VideoData. These two models contain the details of each user and watched videos by that user. I used many-to-many relation. Now I want to filter top 10 v

Solution 1:

Use Count function from django ORM:

    VideoData.objects.annotate(
        watches_count=models.Count('user_set')
    ).order_by('-watches_count')[:10]

Post a Comment for "Annotate Many To Many Relation In Django"