Django-tables2在视图上定义自定义列

时间:2012-06-06 10:17:39

标签: django dynamic dry django-tables2

我想像这样定义一个通用的django-tables2表:

class GenericTable(tables.Table):
    c1 = tables.TemplateColumn("placeholder")

现在,根据我的观点,我想做类似的事情(警告 - 伪代码)

g=GenericTable() # Don't pass data to the table right now
g.columns[0].verbose_name="Specific name based on view"
g.columns[0].template = "Spefic template based on view"
g=initialize( [...] )

有办法吗?

如果不可能,可以在初始化之前更改GenericTable类的定义吗?

我可以定义一些具有不同c1字段但不会干的表:(

1 个答案:

答案 0 :(得分:2)

你可以在init方法中......它是一样的:

class GenericTable(tables.Table):

    def __init__(self, name_based_in_the_view, *args, **kwargs):
        super(GenericTable, self).__init__(*args, **kwargs)
        self.base_columns['c1'].verbose_name = name_based_in_the_view

在您看来:

g = GenericTable(name_based_in_the_view = "Specific name based on view")