Django_tables2: dynamically hide query-based columns

I have a table based on a multi-field model. I also have two TemplateColumns, one for editing a specific object and the other for deleting it. Here is my code:

class EntitetTable(tables.Table):
    edit = tables.TemplateColumn(template_name='azuriranje/izmena.html',
                            orderable=False, visible=False)
    delete = tables.TemplateColumn(template_name='azuriranje/brisanje.html',
                            orderable=False, visible=False)

    class Meta:
        abstract = True
        attrs = {'class': 'paleblue', }

class TipPredmetaTable(EntitetTable):
    class Meta(EntitetTable.Meta):
        model = models.TipPredmeta

Now I have a hierarchy of users in my system, and only users who are accountants can edit and delete data. In doing so, I tried to perform a check in my view to hide two TemplateColumns:

@login_required
def tippredmeta(request):
    try:
        korisnik = request.user.radnik
    except ObjectDoesNotExist:
        return HttpResponseRedirect("/main/")
    queryset = TipPredmeta.objects.all()
    table = TipPredmetaTable(queryset)
    if korisnik.is_kustos:
        table.edit.visible = True
        table.delete.visible = True
    RequestConfig(request).configure(table)
    return render_to_response('azuriranje/tabelaPrikaz.html', {'table': table, },
                              context_instance=RequestContext(request))

However, in the line, table.edit.visible = TrueI get the following exception:

Exception Type:  AttributeError
Exception Value: 'TipPredmetaTable' object has no attribute 'edit'

, : - , . - , __init__, EntitetTabel ( , ), , TemplateColumns. , . , , table.Table, .

+4
1

exclude tables.Table . . - .

( visible=False):

class EntitetTable(tables.Table):
    edit = tables.TemplateColumn(template_name='azuriranje/izmena.html',
                                 orderable=False)
    delete = tables.TemplateColumn(template_name='azuriranje/brisanje.html',
                                 orderable=False)

:

table = TipPredmetaTable(queryset)
if not korisnik.is_kustos:
    table.exclude = ('edit', 'delete',) 
+7

All Articles