Reputation: 17876
Here is the code I add a user to a group
g = Group.objects.get(name='groupname')
g.user_set.add(your_user)
When I delete a User how I remove this user from group?
Upvotes: 26
Views: 23269
Reputation: 554
See documentation https://docs.djangoproject.com/en/stable/topics/auth/#methods
g.user_set.remove(your_user)
Upvotes: 38
Reputation: 504
To remove a specific Group:
group = Group.objects.get(name='groupname')
user.groups.remove(group)
Upvotes: 16
Reputation: 1397
The simplest method to clear all user groups from the user object is
user_obj.groups.clear()
Upvotes: 18