Search code examples
pythondjangodjango-tables2

Conditional column display, django-tables2


I have developed a table, using django-tables2 which shows records of a model. The last column of this table, is an "edit button" which leads user to the "edit page" of its row record.

What I want is that the user can see the edit column only if she has permission to edit the model!

Also I need to mention that currently I'm using SingleTableView to develop table view.


Solution

  • As I mentioned, I am using SingleTableView. So I found out there's a method in SingleTableView class, which according to its docs:

    Allows passing customized arguments to the table constructor.

    So to remove edit column, I added this method in my view class (which was inherited from SingleTableView:

        def get_table_kwargs(self):
                if not self.request.user.has_perm('permission_to_edit'):
                    return {'exclude': ('edit_column',)}
                else:
                    return {}