【问题标题】:Django - List of Dictionaries to Tables2Django - 到 Tables2 的字典列表
【发布时间】:2013-01-16 19:21:22
【问题描述】:

恐怕我是 Django 的新手。

我有一个字典列表,我想用它来填充Tables2 表。我不知道如何调整字典列表以在 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}]

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

【问题讨论】:

    标签: django list dictionary django-tables2


    【解决方案1】:

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

    定义动态模型工厂 创建动态类是内置函数 type()。而不是 在 Python 中定义类的常规语法:

    类人(对象): 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()
        ]   
    

    【讨论】:

      【解决方案2】:

      为您要显示的每种类型的表格创建一个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()
      

      【讨论】:

      • 感谢您的帮助 :) 。问题是,我可以有数千个这样的子类。有没有办法动态创建这样的子类?另外,我之前尝试过制作类似课程的演示 TrialTable,但是当我将它提供给 Table2 时,Table2 抱怨 TrialTable 不是 QuerySet 或 Table。看来我每一步都被围困了!
      猜你喜欢
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多