2017-06-12 42 views
0

我有一個產品列表。我想獲得它所關聯的類別(類別)。 我做了什麼是:如何獲取Django Osacr產品的類別?

 pro = [] #holds list of all the products 
     for p in pro: 
      for procat in p.get_categories(): 
       print(procat) 

,但它與錯誤返回:

'ManyRelatedManager' object is not iterable 

我從這裏DJANGO OSCAR

回答

4

的方法來獲得的ManyToManyField「類別」作爲一個迭代的對象在文檔中指定,您可以嘗試調用.all()方法,例如:

for procat in p.get_categories().all(): 
0
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)