views:

130

answers:

1

I have this view in my app:

def context_detail(request, context_id):
c = get_object_or_404(Context, pk=context_id)
scs = SherdCount.objects.filter(assemblage__context=c).exclude(count__isnull=True)
total = sum(sc.count for sc in scs)
table = []
forms = []
for a in c.assemblage_set.all():
    for sc in a.sherdcount_set.all():
        forms.append(sc.typename)
forms_set = set(forms)
for a in c.assemblage_set.all():
    diko = {}
    diko['assemblage'] = a
    for f in forms_set:
        for sc in a.sherdcount_set.all():
            if f == sc.typename:
                diko[f] = sc.count
            else:
                diko[f] = 0
    table.append(diko)
return render_to_response('tesi/context_detail.html',
    {'context': c, 'total': total, 'sherdcounts': scs, 'table': table, 'forms': forms_set},
    context_instance=RequestContext(request))

The aim of the two for loops would be that of creating a list of dictionaries that holds values of SherdCount.count with reference to the SherdCount.typename foreign key (and I was able to do that, even if the current code is a bit messed up).

The "table" list should contain something like this:

[{<Type: Hayes 61B>: 0, <Type: Hayes 99A-B>: 0, <Type: Hayes 105>: 0, <Type: Hayes 104A>: 0, <Type: Hayes 104B>: 0, <Type: Hayes 103>: 0, <Type: Hayes 91>: 0, <Type: Hayes 91A>: 0, <Type: Hayes 91B>: 0, <Type: Hayes 91C>: 0, <Type: Hayes 91D>: 0, <Type: Hayes 85B>: 0, <Type: Hayes 82A>: 0, <Type: Hayes 76>: 0, <Type: Hayes 73>: 0, <Type: Hayes 72>: 0, <Type: Hayes 70>: 0, <Type: Hayes 68>: 0, <Type: Hayes 67>: 0, <Type: Hayes 66>: 0, <Type: Hayes 62A>: 0, <Type: Hayes 80B>: 0, <Type: Hayes 59>: 0, <Type: Hayes 61A>: 0, <Type: Hayes 91A-B>: 0, <Type: Hayes 58>: 0, <Type: Hayes 50>: 0, <Type: Hayes 53>: 0, <Type: Hayes 71>: 0, <Type: Hayes 60>: 0, <Type: Hayes 80A>: 0, <Type: Hayes Style A2-3>: 0, <Type: Hayes Style B>: 0, <Type: Hayes Style E1>: 1, 'assemblage': <Assemblage: Brescia, Santa Giulia : non periodizzato>}, {<Type: Hayes 61B>: 0, <Type: Hayes 99A-B>: 0, <Type: Hayes 105>: 0, <Type: Hayes 104A>: 0, <Type: Hayes 104B>: 0, <Type: Hayes 103>: 0, <Type: Hayes 91>: 0, <Type: Hayes 91A>: 0, <Type: Hayes 91B>: 0, <Type: Hayes 91C>: 0, <Type: Hayes 91D>: 0, <Type: Hayes 85B>: 0, <Type: Hayes 82A>: 0, <Type: Hayes 76>: 0, <Type: Hayes 73>: 0, <Type: Hayes 72>: 0, <Type: Hayes 70>: 0, <Type: Hayes 68>: 0, <Type: Hayes 67>: 0, <Type: Hayes 66>: 0, <Type: Hayes 62A>: 0, <Type: Hayes 80B>: 0, <Type: Hayes 59>: 0, <Type: Hayes 61A>: 0, <Type: Hayes 91A-B>: 0, <Type: Hayes 58>: 0, <Type: Hayes 50>: 0, <Type: Hayes 53>: 0, <Type: Hayes 71>: 0, <Type: Hayes 60>: 0, <Type: Hayes 80A>: 0, <Type: Hayes Style A2-3>: 0, <Type: Hayes Style B>: 3, <Type: Hayes Style E1>: 0, 'assemblage': <Assemblage: Brescia, Santa Giulia : Periodo IIIA>},

But the many 0 values are obviously wrong. even though there might be some zeros (the empty cells I was referring to)

The question is, once I have built such a list, how do I create a table in the template with all the cells (e.g. 1 row per Type and 1 column per Context, with SherdCount at cells) ?

Steko

+1  A: 

Here's the data structure.

[{<Type1>: 16,
  <Type2>: 10,
  <Type3>: 12,
  <Type4>: 7,
  <Type5>: 0,
  'assemblage': <Assemblage1>},
 {<Type1>: 85,
  <Type2>: 18,
  <Type3>: 21,
  <Type4>: 12,
  <Type5>: 2,
  'assemblage': <Assemblage2>},
 ...]

The problem is that the resulting table must be generated in row order, and this list of dictionaries is in column order. So the list of dicts must be pivoted into row-major order.

from collections import defaultdict
titles = []
cells = defaultdict(list)
for x,col in enumerate(table):
    titles.append( col['assemblage'] )
    for rk in col:
        if rk == 'assemblage': continue # skip the title
        cells[rk][x]= col[rk]

Also, it helps to formalize the results as a list of lists; dictionaries have no inherent order.

final= []
for name in sorted( cells.keys() ):
    final.append( cells[name] )

Here's the template to render titles and final as a table.

<table>
  <tr>
    {% for t in titles %}<th>{{t}}</th>{% endfor %}
  </tr>
  {% for row in final %}
  <tr>
      {% for cell in row %}<td>{{cell}}</td>{% endfor %}
  </tr>
  {% endfor %}
</table>
S.Lott
More or less, it worked. I had to rewrite part of the previous code (which was leaking anyway). defaultdict is great..
steko