Finglish
Finglish

Reputation: 9956

django clear all users from a many-to-many table relationship based on a filter

I am attempting to clear all the users from a table called Team based on two factors:

  1. team_id
  2. a custom variable in the user profile called level

Bellow is my current effort, I am well aware this is does not work but at least it's a starting point.

team = Team.objects.get(pk = team_id)
team_user_list = team.users.all().filter(userprofile__level = 1)
team_user_list.users.clear()

Note: I don't want to delete the users, simply remove them from the team table.

Upvotes: 0

Views: 181

Answers (1)

San4ez
San4ez

Reputation: 8241

team_user_list is queryset, so it doesn't have users attribute. Try

team = Team.objects.get(pk = team_id)
filtered = team.users.filter(userprofile__level = 1)
team.users.remove(*filtered)

Upvotes: 1

Related Questions