[Fixed]-Django Nested Groups: Groups in Groups



This can be done using django-groups-manager:

The following is taken from the documentation. It shows creating a group called ‘F.C. Internazionale Milan’, then adding ‘Staff’ and ‘Players’ sub-groups, then finally adding members to those groups.

from groups_manager.models import Group, Member
fc_internazionale = Group.objects.create(name='F.C. Internazionale Milan')
staff = Group.objects.create(name='Staff', parent=fc_internazionale)
players = Group.objects.create(name='Players', parent=fc_internazionale)
thohir = Member.objects.create(first_name='Eric', last_name='Thohir')
palacio = Member.objects.create(first_name='Rodrigo', last_name='Palacio')


Note that, although the django-group-manager package appears to be actively maintained, this package is based on the django-mptt package, which is marked as unmaintained on the GitHub project.

As mentioned in a previous comment, the django-mptt package may also help implement this functionality.

The documentation provides an example that shows adding a parent attribute to the Group auth model.


import mptt
from mptt.fields import TreeForeignKey
from django.contrib.auth.models import Group

# add a parent foreign key
TreeForeignKey(Group, on_delete=models.CASCADE, blank=True, null=True).contribute_to_class(Group, 'parent')

mptt.register(Group, order_insertion_by=['name'])


The native Django doesn’t provide this opportunity.

Leave a comment