【问题标题】:Django-tables2: Provide a list of dictionary, how to generate a column for each dictionary entryDjango-tables2:提供字典列表,如何为每个字典条目生成一列
【发布时间】:2014-11-19 23:44:15
【问题描述】:

我知道如果我们有一个模型类,我们可以制作一个生成表并使用:

class Meta:
    model = MyModel

显示每个字段。

现在说如果我有一个字典列表,而不是模型,有没有类似的方法可以做到这一点?

(由于有很多不同的字典,可能是动态创建的,我不想每次都创建一个自定义的:-))

【问题讨论】:

  • 我认为你在这里不需要 django_tables2。您可以使用 django 在模板中呈现一个列表。您可以使用 jquery 表在模板中显示数据。

标签: python html django django-tables2


【解决方案1】:

您可以创建自己的继承自 Table 的类,并在其中定义所需的字段。

class JsonTable(Table):
    json_key_1 = Column()
    json_key_2 = Column()

django tables2 也有一个fields 属性,但如果你的数据是一个字典数组,你就不能使用它。

【讨论】:

    【解决方案2】:

    我也一直在做,这是一个粗略的草图。

    在 django_tables2 上的捎带比自己动手要好几英里!

    另外,我将结果连接到 jquery FooTable 插件。

        import django_tables2 as tables
        counter = 0
    
        def generate(li_dict):
    
            #unique classname.
            global counter
            counter += 1
            table_classname = "MyTableClass%s" % (counter)
    
            class Meta:
                #ahhh... Bootstrap
                attrs = {"class": "table table-striped"}
    
            #generate a class dynamically
            cls = type(table_classname,(tables.Table,),dict(Meta=Meta))
    
            #grab the first dict's keys
            li = li_dict[0].keys()
    
            for colname in li:
                column = tables.Column()
                cls.base_columns[colname] = column
    
            return cls
    
        #now, to make use of it...
        li_dict = [dict(a=11,b=12,c=13),dict(a=21,b=22,c=23)]
    
        cls = generate(li_dict)
        table = cls(li_dict)
    
        # below didn't work, wanted a whole bunch of django setup done first.
        # but I fairly confident it would...
    
        print table.as_html()
        >>>django.core.exceptions.ImproperlyConfigured: {% querystring %} requires django.core.context_processors.request to be in your settings.TEMPLATE_CONTEXT_PROCESSORS in order for the included template tags to function correctly.
    
        #this did...
        print "%s" % table
    
        >>><django_tables2.tables.MyTableClass1 object at 0x1070e1090>
    

    【讨论】:

    • 对我来说效果很好。我不喜欢为每个查询生成一个类,但还没有找到更好的解决方案。使用它连接到 django sql-explorer
    【解决方案3】:

    我很抱歉英语不好 :),但是一个可以帮助的想法,实际上我们可以在通用 django table2 中转换一个 numpy(矩阵)。顺便说一句,感谢 Pyeret 的帮助。

    def convert_array_list_dict(arr):
        _list = []
        for i in xrange(0, arr.shape[0]):
            _list.append(dict(enumerate(arr[i,:])))
    
        for i in xrange(0,len(_list)):
    
            for key in _list[i].keys():
                _list[i]["col_" + str(key)] = _list[i].pop(key)
    
        return _list`
    

    上面的这个函数将numpy数组转换为dict列表

        counter = 0
    def list_dict(dict_):
        global counter
        counter += 1
        table_classname = "MyTableClass%s" % (counter)
    
        class Meta:
            attrs = {"class": "paleblue", 'width': '150%'}
    
        cls = type(table_classname, (tables.Table,), dict(Meta=Meta))
    
        list_ = dict_[0].keys()
    
        for colname in list_:
            column = tables.Column()
            cls.base_columns[colname] = column
    
        return cls
    

    这段代码制作了一个通用表格......和

    t = np.loadtxt(doc.document)
    
    tab = convert_array_list_dict(t)
    
    table = list_dict(tab)
    
    
    table_content = table(tab)
    RequestConfig(request, paginate={'per_page': 30}).configure(table_content)
    return render(request,'app/snippets/upload_file.html',{'document':document,'table_content':table_content})
    

    上面我们可以看到如何使用所有代码...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2013-09-11
      • 2021-02-16
      • 2012-07-14
      • 2019-04-20
      • 2021-04-03
      相关资源
      最近更新 更多