vampure
vampure

Reputation: 64

Django: displaying a dynamic raw query

I have a query which is filtered by type, "fruits"

fruits = { apple, orange, mangoes, pineapples, etc }

however in this situation, each type of fruit held different items as well. (for devs: properties of a domain within the server)

http://i46.tinypic.com/35kj1o7.png

note: - the type of fruit can vary by days, some fruits are not in season. - for devs: individual servers may hold a different number of domains - numType field to dynamically determine how many different "fruits" are available that day

main objective: - display and sort available data into tables.

http://i48.tinypic.com/infhip.png

On view.py,I do 2 raw sql queries

  1. Select * from main where cat =='fruit'
  2. Select * from main where cat =='fruit' && name='numType'

thanks in advance!

Upvotes: 0

Views: 430

Answers (2)

vampure
vampure

Reputation: 64

I may be wrong and there could be better ways of doing this, everyone is welcomed to share and discuss.

current flaw: dicts are unordered

on my view.py:

  • I do two queries,

    1. fruit_list = select * from main where type ="fruit"

    // this will tell me the available categories of fruits on sale that day

    1. number_of_cat = select price from main where type ="fruit" and name="numType"
  • // casting the queryset django returned into a list fruitRows = list(fruit_list)

  • lenRow = len(fruitRows)

  • // creating a dict in python masterFruitList = {};

  • // creating keys in dict based on the number of for i in range(int(number_of_cat)): masterFruitList['fruit_Type'+ str(i+1)] = []

  • // adding rows to the dict

  • // add to context return render_to_response('data.html', {'numdo' : masterFruitList })

template.html :

Key point is to use the .items syntax to iterate python dictionaries

{% if numdo %}
    {% for key,value in numdo.items %}
         <p> hey! <b> {{ key }} </b>
            <table class = "tablebord">
                <tr>
                    <th> name </th>
                    <th> type </th>
                    <th> price </th>
                </tr>
                {% for x in value %}
                    <td class = "tablebord"> {{ x.name }} </td>
                    <td class = "tablebord"> {{ x.type }} </td>
                    <td class = "tablebord"> {{ x.price }} </td>
                </tr>
                {% endfor %}
            </table>
         </p>

    {% endfor %}
{% endif %}

Upvotes: 0

Blake
Blake

Reputation: 491

Not sure I entirely follow the question, but try taking a look at the documentation on Django template for loops, you can loop over key/value pairs with that tag.

If it's more complicated than that, can you try giving a little more explanation?


Perhaps regroup will do what you're looking for?

{% regroup fruit_list by [key] as apple_list %}

{% for apples in apple_list %}
    <table>
    {% for apple in apples %}
    <td>{{apple}}</td>
    {% endfor %}
    </table>
{% endfor %}

Upvotes: 2

Related Questions