【问题标题】:Filter tags in relationship with other model in taggit在 taggit 中过滤与其他模型相关的标签
【发布时间】:2014-02-06 12:49:33
【问题描述】:

我有 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

【问题讨论】:

    标签: python django django-taggit


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多