我已经使用Django表开发了一个模板列,但我希望只有超级用户才能查看该列。
MyTables.py
class DeviceTable(tables.Table):
def view(request):
if request.user.is_superuser:
edit = tables.TemplateColumn(template_code)
class Meta:
attrs = {"class": "table table-striped table-hover"}
model = Devices
fields = (
"name",
"location",
"phone_number",
"ip_address",
"created_date",
)
上面的代码不起作用。
答案 0 :(得分:2)
在这种情况下,您可以使用before_render
钩子:
class DeviceTable(tables.Table):
edit = tables.TemplateColumn(template_code)
class Meta:
attrs = {"class": "table table-striped table-hover"}
model = Devices
fields = (
"name",
"location",
"phone_number",
"ip_address",
"created_date",
)
def before_render(self, request):
if request.user.is_superuser:
self.columns.show('edit')
else:
self.columns.hide('edit')