[Answered ]-Django: get most frequent objects in many to many relationship

1👍

You an order the SoundCategorys by the number of items:

from django.db.models import Count

SoundCategory.objects.alias(nitem=Count('sound_reviews')).order_by('-nitem')

or when related to a single SoundItem item:

from django.db.models import Count

SoundCategory.objects.filter(sound_reviews__sound=my_sound_item).alias(
    nitem=Count('sound_reviews')
).order_by('-nitem')

Leave a comment