ManyRelatedManager对象不可迭代

时间:2013-02-17 11:48:34

标签: django

尝试这样做:

已更新:

wishList = WishList.objects.get(pk=20)
matches = [val for val in Store.attribute_answers.all() if val in wishList.attribute_answers]

得到这个......

'ManyRelatedManager' object is not iterable

这两个领域都很多,所以怎么办呢?

6 个答案:

答案 0 :(得分:84)

尝试

matches = [val for val in Store.attribute_answers.all() if val in WishList.attribute_answers.all()]

答案 1 :(得分:44)

听起来你正在寻找像Store.attribute_answers.all()

这样的东西

答案 2 :(得分:10)

对于所有在问题中找到阅读代码的人,例如 TL; DR

代替query_set.many_to_many

您应该使用 query_set.many_to_many.all()

答案 3 :(得分:9)

如果您在模板中执行此操作

  {% for room in study.room_choice.all %}
    {{ room }}
    {% empty %}
    empty list!
  {% endfor %}

答案 4 :(得分:0)

在个人资料模型中,busines_type在这是外键

pro = Profile.object.filter(user=myuser).first()
business_type = pro.business_type.all()
if business_type:
    b_type = ''
    for b in business_type:
        b_type += str(b.type)+' '
        a = b_type

答案 5 :(得分:-1)

每当出现此问题时,我都会不断提出此问题。尤其是当试图在一个函数中实际遍历多个对象时。

作为模板,您可以执行以下操作:

array = many_to_many.all()
for x in many_to_many:
  function here
相关问题