views:

841

answers:

5

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>
A: 

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.

Mike Mazur
this does not work, i set up the outside loop to test it. It doesnt loop at all.
Galen
A: 

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.

Alex Martelli
i have to loop through all the categories and print out each company in its category, then loop through companies and pritn out each video in its company. i cant see a way to do that using that method. ill update my question to illustrate this.
Galen
A: 
Glader
That loop doesn't work with multi-dimensional dictionaries apparently. I've tried that, and variations of it.
Galen
That's strange, because i've run it and have got what you want. Show result of running your dict on this template.
Glader
{% for key, val in videos.items %}<h1>{{ val.name }}</h1>{% endfor %}that doesnt even work
Galen
Read it carefully please. I wrote "v2.videos.items".
Glader
+2  A: 

When moving from one language or framework to another, you need to realise that it's not usually a good idea to write your code in exactly the same way, even if you can.

For example:

I'm creating my dictionary from a sql query

Why are you doing this? The way to represent objects from a database in Django is to use a model. That will take care of a whole lot of stuff for you, including the SQL but will also help with iterating through related tables.

Daniel Roseman
I tried using the built in model functions to do it but i came to the conclusion that it wouldn't work
Galen
But you don't say why. It definitely will, I do this all the time.
Daniel Roseman
this is the very first thing i tried. it doesnt work. category.companies doesnt work. category has no link to company. company has the foreign key to category. i tried using company.set_category, but you cant do that in a django template apparently
Galen
put related_name='companies' in the FK definition, then category.companies will work
FogleBird
+3  A: 

You should be using the built in ORM instead of using your own queries (at least for something simple like this), makes things much easier (assuming you've also built your models in your models.py file)

In your view:

def categories_view(request):
    categories = Categories.objects.all()   #maybe put an order_by or filter here
    return render_to_response("your_template.html", {'categories':categories})

In your template:

{% for category in categories %}
    <h1>{{ category.name }}</h1>
    {% for company in category.company_set.all %}
        <h2>{{ company.name }}</h2>
        <ul>
        {% for video in company.video_set.all %}
            <li>{{ video.name }}</li>
        {% endfor %}
        </ul>
    {% endfor %}
{% endfor %}

I haven't tested it but it should work. Compare this code to what you would have to write if you weren't using ORM, in either PHP or Python.

take a look at the django docs for more info, I'd recommend taking a few hours and doing the tutorial.

Update: modified the code to use "_set.all"

Adrian Mester
+1: Drop the PHP-isms and just use Django the way it was meant to be used.
S.Lott
this is the very first thing i tried. it doesnt work. category.companies doesnt work. category has no link to company. company has the foreign key to category. i tried using company.set_category, but you cant do that in a django template apparently
Galen
category.set_company rather
Galen
Of course you can - that's the whole point. You probably need category.company_set.all If you're having trouble with it, post your code (probably in another question) and we can have a look.
Daniel Roseman
yeah, sorry about that, you need company_set...my point was that you don't need multidimensional dictionaries for this, using the ORM is much simpler
Adrian Mester
i figured out my issues. I was using category.company_set.all() in my templates and i was getting an error. removing the () made everything work fine. thanks
Galen