2013-01-16 35 views
1

害怕我是Django的新手。Django - 表的字典清單2

我有一個詞典列表,我想用它來填充Tables2表。我不知道如何去適應類型的字典列表表2中的工作:(該網站提示:

import django_tables2 as tables 

data = [ 
    {"name": "Bradley"}, 
    {"name": "Stevie"}, 
] 

class NameTable(tables.Table): 
    name = tables.Column() 

table = NameTable(data) 

我不知道這一點還有,我會用這一觀點與許多套不同的!數據,所以我的密鑰將改變視圖

下面是一個字典列表的例子(請注意,下面兩個字典具有相同的鍵;這總是發生在每個視圖中;它只是在不同的視圖中將會是不同組的鑰匙):

[{'trial2_click': u'left', 'timeStored': datetime.time(13, 35, 5), 'runOnWhatHardware': u'bla', 'id': 1L, 'timeStart': datetime.datetime(2012, 11, 2, 12, 54, 58, tzinfo=<UTC>), 'trial1_RT': 234.1, 'approxDurationInSeconds': 123L, 'timeZone': u'UTC', 'expt_id': 2L, 'trial1_click': u'right', 'trial2_RT': 2340L}, {'trial2_click': u'left', 'timeStored': datetime.time(13, 39, 15), 'runOnWhatHardware': u'bla', 'id': 2L, 'timeStart': datetime.datetime(2012, 11, 2, 12, 54, 58, tzinfo=<UTC>), 'trial1_RT': 234.1, 'approxDurationInSeconds': 123L, 'timeZone': u'UTC', 'expt_id': 2L, 'trial1_click': u'right', 'trial2_RT': 2340L}, {'trial2_click': u'left', 'timeStored': datetime.time(15, 32, 59), 'runOnWhatHardware': u'bla', 'id': 3L, 'timeStart': datetime.datetime(2012, 11, 2, 12, 54, 58, tzinfo=<UTC>), 'trial1_RT': 234.1, 'approxDurationInSeconds': 123L, 'timeZone': u'UTC', 'expt_id': 4L, 'trial1_click': u'right', 'trial2_RT': 2340L}] 

會很高漲吃了任何人的幫助:)

回答

0

爲你想要顯示的每種類型的表創建一個Table子類。通過類型我的意思是列的組合。例如:

import datetime 
import django_tables2 as tables 

[ 
    {'trial2_click': u'left', 'timeStored': datetime.time(13, 35, 5), 'runOnWhatHardware': u'bla', 'id': 1L, 'timeStart': datetime.datetime(2012, 11, 2, 12, 54, 58), 'trial1_RT': 234.1, 'approxDurationInSeconds': 123L, 'timeZone': u'UTC', 'expt_id': 2L, 'trial1_click': u'right', 'trial2_RT': 2340L}, 
    {'trial2_click': u'left', 'timeStored': datetime.time(13, 39, 15), 'runOnWhatHardware': u'bla', 'id': 2L, 'timeStart': datetime.datetime(2012, 11, 2, 12, 54, 58), 'trial1_RT': 234.1, 'approxDurationInSeconds': 123L, 'timeZone': u'UTC', 'expt_id': 2L, 'trial1_click': u'right', 'trial2_RT': 2340L}, 
    {'trial2_click': u'left', 'timeStored': datetime.time(15, 32, 59), 'runOnWhatHardware': u'bla', 'id': 3L, 'timeStart': datetime.datetime(2012, 11, 2, 12, 54, 58), 'trial1_RT': 234.1, 'approxDurationInSeconds': 123L, 'timeZone': u'UTC', 'expt_id': 4L, 'trial1_click': u'right', 'trial2_RT': 2340L} 
] 

class TrialTable(tables.Table): 
    trial2_click = tables.Column() 
    timeStored = tables.TimeColumn() 
    runOnWhatHardware = tables.Column() 
    timeStart = tables.DateTimeColumn() 
    trial1_RT = tables.Column() 
    approxDurationInSeconds = tables.Column() 
    timeZone = tables.Column() 
    expt_id = tables.Column() 
    trial1_click = tables.Column() 
    trial2_RT = tables.Column() 
+0

感謝您的幫助:)。問題是,我可以擁有數千個這樣的子類。有沒有辦法動態創建這樣的子類?此外,我試圖做一個類似之前的演示TrialTable類,但是當我將它提交給Table2時,Table2會抱怨TrialTable不是QuerySet或Table。似乎我每一步都被圍攻! – andyw

1

解決我自己的Q,我發現here在運行時動態地使一個類的方法:

定義動態模型工廠的基本原則,使我們能夠 創建動態類是內置的函數類型()。代替 正常語法在Python定義類:

類的人(對象): 名稱的類型()函數可以被用來創建相同類=「朱」,這裏是如何上面看起來的類使用內置的()的類型:

人=類型(「人」,(對象),{「名」:「茱莉亞」})使用類型() 意味着你可以編程確定數量和名稱構成該類的 屬性。

和我的工作代碼:

def getTable(table_name): 
    cursor = connection.cursor() 
    try: 
     cursor.execute("""SELECT * FROM %s,%s;""" %(table_name,'subscription_exptinfo')) # want autoincrement key? 
     exptData = dictfetchall(cursor) 
    except Exception, e: 
     ''  

    attrs = {} 
    cols=exptData[0] 

    for item in cols: 
     attrs[str(item)] = tables.Column() 

    myTable = type('myTable', (tables.Table,), attrs)   

    return myTable(exptData) 

def dictfetchall(cursor): 
    "Returns all rows from a cursor as a dict" 
    desc = cursor.description 
    return [ 
     dict(zip([col[0] for col in desc], row)) 
     for row in cursor.fetchall() 
    ]