Django - 表2的字典列表 [英] Django - List of Dictionaries to Tables2

查看:81
本文介绍了Django - 表2的字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Django来说,害怕我是新手。



我有一个列表,我想用来填充一个 Tables2 表。我不知道如何适应表2中的Dicts列表:(网站建议:

  import django_tables2 as table 

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, ,'test1_RT':234.1,'approxDurationInSeconds':123L,'timeZone':u'UTC','expt_id':4L,'trial1_click':u 'right','trial2_RT':2340L}] 

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

解决我自己的问题,我发现这里一种在运行时动态制作类的方法:


定义动态模型工厂基本原则允许我们
创建动态类是内置函数type()。而不是在Python中定义一个类的
正常语法:



class Person(object):
name =Juliatype()函数可以用来创建同一个类,下面是类的上面使用type()的内置函数:



Person = type(Person,(对象,),{'name':Julia})使用type()
表示您可以编程确定构成该类的
属性的数量和名称。


和我的工作代码:

  def getTable )
cursor = connection.cursor()
try:
cursor.execute(SELECT * FROM%s,%s;%(table_name,'subscription_exptinfo') )#想自动增加密钥?
exptData = dictfetchall(cursor)
除了异常,e:
''

attrs = {}
cols = exptData [0]

在cols中的项目:
attrs [str(item)] = tables.Column()

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

返回myTable(exptData)

def dictfetchall(cursor):
将光标中的所有行作为dict返回
desc = cursor.description
return [
dict(zip([col [0] for col in desc],row))
for cursor.fetchall()
]


afraid I'm a Newbie when it comes to Django.

I have a list of Dictionaries which I want to use to populate a Tables2 table. I don't know how to adapt the list of Dicts to work in Table2 :( The website suggests:

import django_tables2 as tables

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

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

table = NameTable(data)

I can't figure this out! Also, I will be using this view with many different sets of data and so my keys will change over views.

Here's an example of a list of Dictionaries (note that below, the two Dictionaries have the same keys; this always happens in each view; it is just that in different views there will be different sets of keys):

[{'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}]

Would much appreciate anyone's help :)

解决方案

solving my own Q, I found here a way of dynamically making a class at runtime:

Defining a dynamic model factory The basic principle that allows us to create dynamic classes is the built-in function type(). Instead of the normal syntax to define a class in Python:

class Person(object): name = "Julia" The type() function can be used to create the same class, here is how the class above looks using the type() built-in:

Person = type("Person", (object,), {'name': "Julia"}) Using type() means you can programatically determine the number and names of the attributes that make up the class.

and my working code:

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

这篇关于Django - 表2的字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆