Note that there are some explanatory texts on larger screens.

plurals
  1. POTraversing multi-dimensional dictionary in django
    primarykey
    data
    text
    <p>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.</p> <p>So I can create multi-dimensional arrays using <a href="http://parand.com/say/index.php/2007/07/13/simple-multi-dimensional-dictionaries-in-python/" rel="nofollow noreferrer">this</a>, but i can't loop it in a django template. <a href="http://code.activestate.com/recipes/389639/" rel="nofollow noreferrer">this</a> doesnt work but i imagine i cant loop through it if i could get it to work.</p> <pre><code>{% for key,val in dictionary.items %} </code></pre> <p>only works for actual dictionaries it seems, not the custon multi-dimensional dictionary classes. </p> <p>I'm creating my dictionary from a sql query:</p> <pre><code>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 ] </code></pre> <p>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.</p> <p>I thought maybe i could do with the backwards lookups in django using set_MODEL but i couldn't figure that out either.</p> <p>Any help on how to accomplish my goal would be appreciated. I hope my question is clear</p> <h2>EDIT:</h2> <p>When im done looping in my template it looks like this...</p> <pre><code>&lt;h1&gt;Category&lt;/h1&gt; &lt;h2&gt;Company&lt;/h2&gt; &lt;ul&gt; &lt;li&gt;video&lt;/li&gt; &lt;/ul&gt; &lt;h2&gt;Company&lt;/h2&gt; &lt;ul&gt; &lt;li&gt;video&lt;/li&gt; &lt;li&gt;video&lt;/li&gt; &lt;/ul&gt; &lt;h1&gt;Category&lt;/h1&gt; &lt;h2&gt;Company&lt;/h2&gt; &lt;ul&gt; &lt;li&gt;video&lt;/li&gt; &lt;/ul&gt; &lt;h2&gt;Company&lt;/h2&gt; &lt;ul&gt; &lt;li&gt;video&lt;/li&gt; &lt;li&gt;video&lt;/li&gt; &lt;/ul&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload