0

I have a number of Lists where each List contains a number of Users, and I would like to remove a certain user from all Lists.

I was trying to do this in my views.py but it is not workin and I'm not sure why. I started looping through all lists, for each list check if the user belongs to that list, remove the user from the list. Otherwise just set a message. Here is my code for this:

def delUserFromList(user_id):
    user = User.objects.get(pk=user_id)

    for list_id in List.objects.all() :
         if user.user_lists.filter(pk=list_id).exists():
            list_id.user.remove(user)
            message = "Success!"
         else:
            message = "User does not exist on this list!"

What am I doing wrong here? Is it not ok that for each list I check whether the user has that list and then remove?

Thanks for the help!

Edit: I was missing from list.models import List in my views.py file.

user2573690
  • 5,493
  • 9
  • 43
  • 61

1 Answers1

0

Based on this comment:

def del_user_from_lists(user):
    user.user_lists.through.objects.filter(user_id=user.pk).delete()
Community
  • 1
  • 1
avd
  • 318
  • 1
  • 7