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>
Was it helpful?

Solution

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"

OTHER TIPS

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.

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 %}
    <h1>{{ val.cat_name }}</h1>
    {% for k2, v2 in val.companies.items %}
        <h2>{{ v2.comp_name }}</h2>
        <ul>
        {% for k3, v3 in v2.videos.items %}
            <li>{{ v3.vid_name }}</li>
        {% endfor %}
        </ul>
    {% endfor %}
{% endfor %}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top