I have a Django model defined as follows:
class Session(models.Model):
...
leader = models.ForeignKey(User, related_name='leader')
follower = models.ForeignKey(User, related_name='follower')
...
Now let's say I have a QuerySet of Session objects:
some_sessions = Session.objects.filter(some_criteria=True)
How can I get a simple count of unique Users that have been either a leader or follower in any Session in some_sessions?
For example, a particular User may have been a leader in some number of Session objects, but a follower in a handful of other Session objects. That particular User should only contribute 1 to the desired unique Users count.
There are of course some naive solutions to this, but for performance reasons, I'm wondering if there's a solution that leverages the power of QuerySets a bit more, if possible.
EDIT: To clarify, here's a naive method that gives the result I need:
active_user_ids = set()
some_sessions = Session.objects.filter(some_criteria=True)
for session in some_sessions:
active_user_ids.add(session.leader.id)
active_user_ids.add(session.follower.id)
all_active_users = User.objects.filter(id__in=active_user_ids).count()