[Django]-Django – cannot unpack non-iterable ManyRelatedManager object

6👍

Replace:

items = Item.objects.filter(user_obj.items)

With:

user_items = Items.objects.filter(profile=user_obj)
items = Items.objects.exclude(pk__in=user_items)

user_items will contain a queryset with all Item objects related to the User in user_obj.

items will contain a queryset with all Item objects, excluding the ones that are related to the User in user_obj

You were getting your error because filter() needs a field from a model:

  • filter(user='some user')

More information about how to make queries can be read in the docs

👤Stevy

1👍

try using:

from django.shortcuts import get_list_or_404

items = get_list_or_404(Item, user=user_obj) 

if that doesn’t work, trying uing,

from django.shortcuts import get_object_or_404

items = get_object_or_404(Item, user=user_obj) 
👤Jay

Leave a comment