Django/Python - Collect the data to the right form (Algorithm) -
i have models this:
item: name desc attrgroup: name order attrname: name group.foreinkey(attrgroup) order attrval: value attr.foreinkey(attrname) item.foreinkey(item)
so want compare attributes of n items in list items_id=[1,3,7,...]
in views, likes this:
attrs = attrval.object.filter(item__id__in=items_id)
and send templates. i'm gonna confusing way how arrange list interface.
the templates should this:
<table> {% g in group %} <tr class="group_name">{{ g.name }}</tr> {% in attrs %} <tr> <td>{{ a.name }}</td> {% in items_id %} <td>{{ value of item 'i' }}</td> { %endfor %} </tr> {% endfor %} {% endfor %} </table>
i think have solution issue. thanks!
update: follow pretty solution @lott, found out way how show data templates. i'm using code likes this:
{% g in groups %} <tr class="title_row"> <td class="group_name" colspan="{{ no_items }}">{{ g.0 }}</td> </tr> {% in g.1 %} <tr> <td>{{ a.attr.name }}</td> {% in comparing_items %} <td>{% if a.item.id == %}{{ a.value }}{% endif %}</td> {% endfor %} </tr> {% endfor %} {% endfor %}
and appears:
<table> <tr> <td>attr name 1</td> <td>attr value of item 1</td> <td></td> <td></td> <td></td> </tr> <tr> <td>attr name 2</td> <td>attr value of item 1</td> <td></td> <td></td> <td></td> </tr> <tr> <td>attr name 1</td> <td></td> <td></td> <td></td> <td>attr value of item 4</td> </tr> </table>
my goal list same attribute of items [1,2,3,...] in same row. html should this:
<table> <tr> <td>attr name 1</td> <td>attr value of item 1</td> <td>attr value of item 2</td> <td>attr value of item 3</td> <td>attr value of item n</td> </tr> <tr> <td>attr name 2</td> <td>attr value of item 1</td> <td>attr value of item 2</td> <td>attr value of item 3</td> <td>attr value of item n</td> </tr> <tr> <td>attr name m</td> <td>attr value of item 1</td> <td>attr value of item 2</td> <td>attr value of item 3</td> <td>attr value of item n</td> </tr> </table>
thank much!
in view function can can find attrname , attrgroup each attrvalue in attrs
query set. create relevant groups in view function. display details in template.
groups = defaultdict( list ) in attrs: groups[a.attr.group.order, a.attr.group.name].append( ) group_list = [ (name[1], groups[name]) name in sorted( groups.keys() ) ]
your template this
<table> {% g in group_list %} <tr class="group_name">{{ g.0 }}</tr> {% in g.1 %} <tr> <td>{{ a.name }}</td> {% in a.item %} <!-- yes, can refer methods of model objects. --> <td>{{ }}</td> { %endfor %} </tr> {% endfor %} {% endfor %} </table>
Comments
Post a Comment