Monday, July 14, 2008

'ManyRelatedManager' object is not iterable

When I'm writing an app ,in fact it's just a test.following is the actually code:
{% if query%}
{%for book in results%}

{{book.title}}


{%for author in book.authors%}
  • {{author.first_name}}|{{author.last_name}}

  • {%endfor%}

    {{book.publisher.name}}

    {%endfor%}
    {%else%}
  • No Books Found

  • {%endif%}

    When I run it.I got an error message:
    'ManyRelatedManager' object is not iterable.

    the solution for this is

    change the for loop syntax to:

    {%for author in book.authors.all%}

    then it'll be done.

    20 comments:

    LOLHAT said...

    You just made my day :)

    Thom Allen said...

    Mine too.

    Jean Machuca said...
    This comment has been removed by the author.
    Jean Machuca said...

    The ManyRelatedManager object have a "all" method that returns the children array:

    Example:

    If you are defined a child list like this:

    class myParent(models.Model):
    aChildList = ManyToMany('myChildModel')

    then, for read this list of child attribute do:

    tmpChildList = myParent.aChildList.all()
    for myChild in tmpChildList:
    print myChild.name # if name is a field of the child

    best regards!

    Unknown said...

    thanks !
    Saved me some time

    Unknown said...

    Superhelpful. Thanks for posting.

    discodj said...

    thank you thank you thank you thank you thank you thank you thank you

    Unknown said...

    Thanks! I was stuck on that!

    the Same said...

    Thank you so much!

    Patti said...

    I love when I use Google correctly. Thanks for giving me the exact answer I was looking for.

    Unknown said...

    Great info. Please note that in the original post, all is used as a property. As Jean Machuca pointed out this is actually a method e.g. all()

    Ketaki said...
    This comment has been removed by the author.
    Ketaki said...

    thanks!

    Dan Lewis said...

    Thanks! This is the first result when Googling "manyrelatedmanager". Nice.

    h'na said...

    Thank you so much! :)

    garmoncheg said...

    Thanks so mush! Saved som time!

    Carlos Ramos said...

    Thanks!

    Flo inna L.A. said...

    Thank you dude !!!

    Jonas Porto said...

    So helpful for long time yet, thank you for sharing.
    greetings from Brazil.

    Andrew said...

    nice one - this saved me some time