python – TypeError:’RelatedManager’对象不可迭代

Django

我有下一个型号:

class Group(models.Model):
    name = models.CharField(max_length=100)
    parent_group = models.ManyToManyField("self", blank=True)

    def __unicode__(self):
        return self.name


class Block(models.Model):

    name = models.CharField(max_length=100)
    app = models.CharField(max_length=100)
    group = models.ForeignKey(Group)

    def __unicode__(self):
        return self.name

例如,块b1具有g1组。根据它的名字我想得到组g1的所有块。我写了下一个递归函数:

def get_blocks(group):

    def get_needed_blocks(group):
        for block in group.block_set:
            blocks.append(block)

        if group.parent_group is not None:
            get_needed_blocks(group.parent_group)

    blocks = []
    get_needed_blocks(group)
    return blocks

但是b1.group.block_set返回了RelatedManager对象,它不是可迭代的。

该怎么办?怎么了?

尝试这个:

block in group.block_set.all()
http://stackoverflow.com/questions/6314841/typeerror-relatedmanager-object-is-not-iterable

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:python – TypeError:’RelatedManager’对象不可迭代