I have a queryset at the moment that returns the number items in a model ordered by the number of people watching it. So I have a m2m field representing this link.
Ie:
#models.py
class MyModel(models.Model):
...
watchers = models.ManyToManyField(User, blank=True)
I would count the number of occurrences and order them by count in a default manager which was then used by the view.
Now I'm going over to using django-notification, using 'notification.ObservedItem' to allow users to watch an instance if MyModel.
So in my view when a user posts some content I have something like this:
notification.observe(object, request.user, 'new_object')
This works well.
Now, how can I generate a queryset representing all the objects of class MyModel, ordered by the number of people 'observing' them?
You can accomplish that by using annotations:
from django.db.models import Count
MyModel.objects.annotate(num_users=Count('watchers')).order_by('num_users')