Reputation: 1462
I have a list of product. I want to get the category(categories) it is associated with. What i have done is :
pro = [] #holds list of all the products
for p in pro:
for procat in p.get_categories():
print(procat)
but it returns with Error:
'ManyRelatedManager' object is not iterable
I got the method from here DJANGO OSCAR
Upvotes: 0
Views: 898
Reputation: 1983
from oscar.core.loading import get_model
Product = get_model('catalogue', 'Product')
ProductCategory = get_model('catalogue', 'ProductCategory')
cat_ids = Product.objects.values_list('categories', flat=True)
categories = ProductCategory.objects.filter(pk__in=cat_ids)
Upvotes: 0
Reputation: 301
To get an iterable object for the ManyToManyField "categories" as specified in the docs, you can try to call .all() method, e.g.:
for procat in p.get_categories().all():
Upvotes: 5