2009-03-19 5 views
1

나는 내 응용 프로그램에서이보기가 있습니다값의 사전을 기반으로 빈 셀이있는 테이블을 쓰기

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)) 

루프에 대한 두 가지의 목적은 SherdCount의 값을 보유하고 사전의 목록을 만드는 그 것을 .count SherdCount.typename 외래 키 참조 (현재 코드가 엉망인 경우에도 가능).

[{<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>}, 

그러나 많은 0 값은 분명히 잘못 :

은 "테이블"목록

는 다음과 같이 포함되어야합니다. 비록 일부 0이있을지라도 (예를 들어 내가 말했던 빈 셀)

그런 목록을 작성한 후에는 모든 셀 (예 : 1 행당 1 행)의 테이블을 어떻게 만들 수 있습니까? 셀당 SherdCount와 함께 컨텍스트 당 유형 및 1 열)?

Steko

+0

결과 테이블 구조의 샘플을 보여주는 것이 도움이됩니다. –

+0

물론입니다. http://www.linux.it/~steko/static/tesi/contexts/9/index.html 다음은 "Forme"제목 아래에있는 기본 데이터의 예입니다. http://paste.pocoo.org/show/108661/과 같은 것을 원합니다. (순진하지만 아이디어를줍니다.) – steko

+0

'table' 변수의 값 샘플로 질문을 업데이트 할 수 있습니까? –

답변

2

다음은 데이터 구조입니다.

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

결과 테이블을 행 순서로 생성해야하며이 사전 목록은 열 순서로 작성해야합니다. 따라서 dicts 목록은 행 우선 순서로 피벗되어야합니다.

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] 

또한 결과를 목록의 목록으로 형식화하는 데 도움이됩니다. 사전에는 고유 한 순서가 없습니다.

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

여기에 테이블로 titlesfinal을 렌더링 할 수있는 템플릿입니다.

<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> 
+0

다소 많았지 만 효과가있었습니다. 이전 코드 (어쨌든 새고 있던 코드)의 일부를 다시 작성해야했습니다. 기본 명령은 훌륭합니다 .. – steko

관련 문제