过滤标签与taggit中的其他模型的关系

时间:2014-02-06 04:43:42

标签: python django django-taggit

我有2个型号:评论和产品:

class Review(models.Model):
    user = models.ForeignKey(User, related_name="user_blog")
    tag = TaggableManager() 
    product = models.ForeignKey(Product)
    review_text = models.TextField() 
    created = models.DateTimeField(auto_now=True, auto_now_add=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=True)
    hashtag = models.ForeignKey(Hashtag)

class Product(models.Model):
    name = models.CharField(max_length=500)

我想过滤与产品相关的所有标签,我这样做:

review = Review.objects.get(product=prod.id)  
for tag in review.tag:
    print tag.name

但我得到了:

 Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: '_TaggableManager' object is not iterable

1 个答案:

答案 0 :(得分:3)

Manager不可迭代。使用all方法获取可迭代的QuerySet

for tag in review.tag.all():
    print tag.name