I want to find all users which are at least in one group.
I found a solution
from django import setup
setup()
from django.contrib.auth.models import User
user_without_group = User.objects.update_or_create(username='user-without-group')[0]
user_without_group.groups.clear()
query = User.objects.filter(groups__isnull=False).values_list('username', flat=True)
print (query.query)
print list(query)
assert user_without_group.username not in query
... but the solution is not nice. Is there a more obvious solution than groups__isnull=False
?
update
Why I think this solution is not nice: I think this is not obvious. It is not easy to read and understand. If you show this to someone who knows Python, but has never used the django ORM, I don't think he will immediately understand what this means.
User.objects.filter(groups__isnull=False)