I have a table that's based on a model that has several fields. I also have two TemplateColumn
s, one for editing the specific entity and another 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 user hierarchy in my system and only users that are bookkeepers can edit and delete the data. That being said, I tried to implement a check in my view to hide the two TemplateColumn
s:
@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, I get the following exception on the table.edit.visible = True
line:
Exception Type: AttributeError
Exception Value: 'TipPredmetaTable' object has no attribute 'edit'
Now, here are the things I've tried:
- First I thought about using fields and exclude, but I couldn't alter that dynamically.
- Then I thought about placing all of this into the __init__
method, effectively making edit and delete attributes of my EntitetTabel (the idea was to solve the error) but while the error was gone, so were my TemplateColumns. I tried displaying them via fields, but that didn't help. My guess is that the superclass, the tables.Table, doesn't work like that.