Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 652
Martin Taleski Avatar asked Sep 06 '14 00:09

Martin Taleski


1 Answers

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

user.groups.clear()
like image 118
Paulo Almeida Avatar answered Nov 20 '22 03:11

Paulo Almeida