[Answer]-Django determine "most popular"

1๐Ÿ‘

โœ…

Iโ€™m too lazy to create a django project from scratch, but this one should do the job:

from django.db.models import Count
MyModel.objects.annotate(Count('myuser'))

(or this)

MyModel.objects.annotate(Count('myuser_set'))

if not, try this:

class MyUser(models.Model):
    favorite_models = models.ManyToManyField(MyModel, related_name='myuser')

and then

MyModel.objects.annotate(Count('myuser_set'))

(let me know if it works, in any case this page should contain what you need to do that: https://docs.djangoproject.com/en/dev/topics/db/aggregation/)

๐Ÿ‘คredShadow

Leave a comment