unsafe_where_true
unsafe_where_true

Reputation: 6320

Optimize a drop down for django 1.3

Trying to optimize a django application which is using 1.3. Migrating to latest django is not yet an option, as it's a huge application.

So there's this code in the template:

<select id="item_product">
      {% for ip in items %}
      <option value="{{ ip.program.id }}/{{ ip.sector.id }}/">{{ ip }}</option>
      {% endfor %}
    </select>

This seems to generate DB calls for every option - making it pretty slow to just load a page with just a drop-down and a button! Replacing the option values with dummy strings indeed immediately loads the page.

The view is very simple:

@render_to('pick_item.html')
def pick_item(request):
    person = request.user.get_profile()
    items = ItemProduct.objects.filter(program__in=person.programs)
    return {'items': items }

And this code returns pretty fast.

How can I optimize this code for django 1.3 so that the drop-down options have the ids I need more efficiently?

Upvotes: 0

Views: 150

Answers (1)

Kamil Rykowski
Kamil Rykowski

Reputation: 1459

If you only need ID's:

    <select id="item_product">
    {% for ip in items %}
        <option value="{{ ip.program_id }}/{{ ip.sector_id }}/">{{ ip }}</option>
    {% endfor %}
    </select>

If you need something more complex:

    @render_to('pick_item.html')
    def pick_item(request):
        person = request.user.get_profile()
        items = ItemProduct.objects.filter(program__in=person.programs).select_related('program', 'sector')
        return {'items': items }

Upvotes: 3

Related Questions