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.