-->

Django的 - 字典来Tables2名单(Django - List of Dictionari

2019-07-18 07:44发布

怕我是一个新手,当涉及到Django的。

我有我想要使用来填充词典列表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}]

将非常感激任何人的帮助:)

Answer 1:

解决我自己的Q,我发现这里在运行时动态地使一个类的方法:

定义动态模型工厂的基本原则,即允许我们创建动态类的内置功能型()。 而不是正常的语法定义在Python类:

Person类(对象):名称=“朱莉娅”类型()函数可用于创建同一类,这里是上面的类看起来如何使用内置的()的类型:

人=类型(“人”,(对象),{“名”:“茱莉亚”})使用类型()意味着你可以编程方式确定组成的类属性的数量和名称。

和我的工作代码:

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


Answer 2:

创建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()


文章来源: Django - List of Dictionaries to Tables2