Given this somewhat simplified representation of my application’s models, my question is how do I globally find the most popular MyModel? I.e., those MyModels are favorited the most by MyUsers.
I’ve come across similar blog posts about how to find favorite tags, but I don’t think those apply to this particular situation.
class MyUser(models.Model):
favorite_models = models.ManyToManyField(MyModel)
...
class MyModel(models.Model):
name = models.CharField(...)
...
Can this be done in a single query? Or do I need to loop over every MyUser and MyModel to determine the most popular? Thanks in advance!
I’m too lazy to create a django project from scratch, but this one should do the job:
(or this)
if not, try this:
and then
(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/)