Django-tables2 define custom column on view
-
13-06-2021 - |
Pergunta
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 :(
Solução
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")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow