Search code examples
pythondjangousergroups

Display users without a group in a template Django


I am creating an app that allows me to edit users on the frontend. Before doing this I wish to have all the users listed in a page. They are categorized by their group. Users that are in a group show up how they are supposed to. I want to also display users that are not in a group.

This is how I fetch the users in a specific group: groupuser = User.objects.filter(groups__name='mygroup').

How do I display a user without any group in my template?

I am new to Django and don't know much about this.


Solution

  • You can use the field lookup __isnull.

    User.objects.filter(groups__isnull=True) gives you all the users that do not belong to any group.