1👍
✅
If you use Django’s Group
, you access the set of users with:
{% for group in groups %}
{% for user in group.user_set.all %}
…
{% endfor %}
{% endfor %}
You can further boost efficiency by prefetching all the related users with one extra query, so in the view you work with:
groups = Group.objects.prefetch_related('user_set')
Source:stackexchange.com