Traversing multi-dimensional dictionary in django

By : Galen
Source: Stackoverflow.com
Question!

I'm a PHP guy on my first day in Python-land, trying to convert a php site to python (learning experience), and I'm hurting for advice. I never thought it would be so hard to use multi-dimensional arrays or dictionaries as you pythoners call them.

So I can create multi-dimensional arrays using this, but i can't loop it in a django template. this doesnt work but i imagine i cant loop through it if i could get it to work.

{% for key,val in dictionary.items %}

only works for actual dictionaries it seems, not the custon multi-dimensional dictionary classes.

I'm creating my dictionary from a sql query:

vid[ video[ 7 ] ][ 'cat_short_name' ] = video[ 2 ]
vid[ video[ 7 ] ][ 'cat_name' ] = video[ 1 ]
vid[ video[ 7 ] ][ 'cat_id' ] = video[ 7 ]

vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_short_name' ] = video[ 5 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_name' ] = video[ 4 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_website' ] = video[ 6 ]

vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'top_video' ] = 0
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_id' ] = video[ 8 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_name' ] = video[ 9 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_url' ] = video[ 10 ]

I basically need to get all companies in a certain category and then get all videos in that company so i can nest them easily in my template. This is how i did it in php, creating one huge deep array. Trying to duplicate in Python has proven difficult.

I thought maybe i could do with the backwards lookups in django using set_MODEL but i couldn't figure that out either.

Any help on how to accomplish my goal would be appreciated. I hope my question is clear

EDIT:

When im done looping in my template it looks like this...

<h1>Category</h1>
   <h2>Company</h2>
   <ul>
        <li>video</li>
    </ul>
    <h2>Company</h2>
    <ul>
        <li>video</li>
        <li>video</li>
    </ul>
<h1>Category</h1>
   <h2>Company</h2>
   <ul>
        <li>video</li>
    </ul>
    <h2>Company</h2>
    <ul>
        <li>video</li>
        <li>video</li>
    </ul>
By : Galen


Answers

I'm also a Django beginner...

You should be able to nest the for loops to get something like this:

{% for key,val in dictionary.items %}
    {% for key,val in val.items %}

and so on.



If you built your complicated dictionary in the following way:

vid[ video[ 7 ], 'cat_short_name' ] = video[ 2 ]
vid[ video[ 7 ], 'cat_name' ] = video[ 1 ]
vid[ video[ 7 ], 'cat_id' ] = video[ 7 ]

vid[ video[ 7 ], 'companies', video[ 14 ], 'comp_short_name' ] = video[ 5 ]

etc, would that help? The key in this case would be a tuple (with 2 items in the first three cases, 4 items in the fourth), and I'm not sure how you mean to treat it, but the loop on items to get key and value, per se, should work fine.



{% for key, val in vid.items %}
    
By : Glader


This video can help you solving your question :)
By: admin