[Answered ]-Django Admin restrict users to be added to more than one group

1👍

Yes, it is possible to restrict the number of groups a user can be associated with in Django. One way to do this is to override the save_model() method in your custom UserAdmin class and perform the check there, so:

@admin.register(User)
class UserAdmin(DjangoUserAdmin):
    # ... other stuff
    # ... other stuff

    def save_model(self, request, obj, form, change):
        if obj.groups.count() > 1:
            self.message_user(request, _('Users can only be associated with one group.'), level=messages.ERROR)
        else:
            super().save_model(request, obj, form, change)

Leave a comment