Django - Tables2的字典列表

时间:2013-01-16 19:21:22

标签: django list dictionary django-tables2

害怕我是Django的新手。

我有一个字典列表,我想用它来填充Tables2表。我不知道如何调整Dicts列表在Table2中工作:(网站建议:

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}]

非常感谢任何人的帮助:)。

2 个答案:

答案 0 :(得分:1)

解决我自己的问题,我发现here是一种在运行时动态创建类的方法:

  

定义动态模型工厂允许我们的基本原则   create dynamic classes是内置函数type()。而不是   在Python中定义类的普通语法:

     

class Person(object):       name =“Julia”type()函数可以用来创建同一个类,下面是上面的类看起来如何使用内置的type():

     

Person = type(“Person”,(object,),{'name':“Julia”})使用type()   意味着你可以以编程方式确定数量和名称   构成班级的属性。

和我的工作代码:

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

答案 1 :(得分:0)

为要显示的每种类型的表创建一个Table子类。通过 type 我的意思是 set-of-columns 。例如:

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()
相关问题