Martin Taleski
Martin Taleski

Reputation: 6458

Django remove user from all groups

I want to remove a user from all the groups that is assigned to. I am using the standard auth app in Django.

So far I am able to delete one group at a time with:

user.groups.remove(group)

but this adds a lot of sql overhead. I understand that this is an many-to-many relation, but I am not able to find which model represents the many to many mapping and call the delete method from there.

I would like to execute the following query with the Django ORM:

delete from auth_user_group where user_id = 123

Upvotes: 16

Views: 7892

Answers (1)

Paulo Almeida
Paulo Almeida

Reputation: 8071

I don't know what is the SQL, but you can use the clear method:

user.groups.clear()

Upvotes: 28

Related Questions