How to define dynamic number of columns in django-datatables-view

709 Views Asked by At

I'm using the django-datatables-view (that is used in conjunction with the jQuery plugin DataTables). For normal use, I create a class-based view based on BaseDatatableView and define there the columns variable:

class MyCBV(LoginRequiredMixin, BaseDatatableView):
    model = myModel
    columns = ['my_model_column1','my_model_column2']
    ...

Now I would like to create a variable number of columns that is based on a parameter. I can access the parameter in the CBV functions but it seems I cannot overwrite the columns variable. For example, I tried to overwrite the columns variable in get_context_data function but columns retains its original value later in render_column function.

Somebody has a solution?

1

There are 1 best solutions below

0
On BEST ANSWER

BaseDatatableView implements DatatableMixin which has a method get_columns with a default implementation of

def get_columns(self):
    """ Returns the list of columns that are returned in the result set
    """
    return self.columns

You can override it and pass your custom columns here (using your parameter)