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对象,这是不可迭代的。
该怎么办?怎么了?