문제

I want to define a generic django-tables2 Table like this:

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

Now, on my views I'd like to do something like that (warning - pseudocode):

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

Is there a way to do that ?

If that is not possible, is it possible to change the definition of the GenericTable class before initializing it ?

I could define a number of tables with different c1 fields but that wont' be DRY :(

도움이 되었습니까?

해결책

You can in in the init method.... it's the same:

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

In your view:

g = GenericTable(name_based_in_the_view = "Specific name based on view")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top