如何使用django-tables2加载非结构化非查询集数据 [英] how to load unstructured non-queryset data with django-tables2

查看:52
本文介绍了如何使用django-tables2加载非结构化非查询集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用django-tables2显示电子表格或csv文件中的数据.数据将始终是动态的,因此我需要一种将列动态添加到django-tables2表的方法.从文档中看,似乎没有办法.

I'd like to use django-tables2 to display data from a spreadsheet or csv file. The data will always be dynamic so I need a way of dynamically adding columns to my django-tables2 table. From the documentation there seems to be no way of doing this.

有什么想法吗?

推荐答案

在Python中,您可以使用type来

In Python, you can use type to construct classes dynamically.

让我们使用文档中的示例,该表定义了一个只有 name 一栏的表.

Let's use the example from the docs, which defines a table with one column, name.

import django_tables2 as tables

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

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

可以使用以下方式动态定义

This could be defined dynamically with

NameTable = type('NameTable', (tables.Table,), {'name': tables.Column()})

电子表格中的数据会更复杂,但是相同的方法应该可以工作.

The data in your spreadsheet will be more complicated, but the same approach should work.

这篇关于如何使用django-tables2加载非结构化非查询集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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