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

'hasOwnProperty' in javascript -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

How to understand 2 main() functions after using uftrace to profile the C++ program? -