python - Looping over all objects and removing object where ids match? -


i have number of lists each list contains number of users, , remove user lists.

i trying in views.py not workin , i'm not sure why. started looping through lists, each list check if user belongs list, remove user list. otherwise set message. here code this:

def deluserfromlist(user_id):     user = user.objects.get(pk=user_id)      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 not exist on list!" 

what doing wrong here? not ok each list check whether user has list , remove?

thanks help!

edit: missing from list.models import list in views.py file.

based on this comment:

def del_user_from_lists(user):     user.user_lists.through.objects.filter(user_id=user.pk).delete() 

Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -