Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'RelatedManager' object is not iterable

Django

I have next models:

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 

say, block b1 has g1 group. By it's name I want to get all blocks from group g1. I wrote next recursive function:

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 

but b1.group.block_set returns me RelatedManager object, which is not iterable.

What to do? What's wrong?

like image 386
megido Avatar asked Jun 11 '11 08:06

megido


1 Answers

Try this:

block in group.block_set.all() 
like image 50
Andrey Fedoseev Avatar answered Sep 19 '22 16:09

Andrey Fedoseev