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.
You can use the exclude
attribute of the tables.Table
for excluding unwanted fields. It also works after creating the table instance. So in your case, you can do something like this.
First leave your columns visible (i removed the 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)
Then based on your condition, you can exclude the edit and delete fields:
table = TipPredmetaTable(queryset)
if not korisnik.is_kustos:
table.exclude = ('edit', 'delete',)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With