How to split django groups into subsets
I need to split django groups into logical subsets in order to distinguish the ones that I use for administration from the others (such as standard groups and so on).
Reading the code I found that django Group model has only two fields: name and permissions.
I'd like to do something like:
and so on. Is there any built-in function to do that or I'll have to subclass the Group model on my own and add a type field?
Any other suggestion is well accepted!
Instead of subclassing groups, I recommend adding a GroupType model that will have a many to many relation to group. Something like this:
class GroupType(models.Model): name = models.CharField(max_length=128) groups = models.ManyToManyField('auth.Group', related_name='groups' )
Now, if you want to add a group to a specific type you can just do a
group = Group.get(...) group_type = GroupType.objects.get(name='group_type_1') group_type.groups.add(group)
I'm aware that the above is not perfect from a design point of view, since you allow groups to belong to more than one type (of course depending on your requirement this may actually be needed) but you don't mess at all with the stock Django groups. You can easily enforce only one GroupType per Group by using the m2m-changed signal (https://docs.djangoproject.com/en/1.7/ref/signals/#m2m-changed): Check if action is pre_add and, if there are more than one members to this relation throw a ValidationError