我使用默认的django模型(用户和群组),并尝试在使用class UserTable(tables.Table):
selection = tables.CheckBoxColumn(accessor="pk",
attrs={"th__input": {"onclick": "toggleall(this)"}}, orderable=False)
username = tables.LinkColumn('edituser', args=[A('pk')])
first_name = tables.Column()
last_name = tables.Column()
group = tables.Column(accessor='user.groups.group_id')
class Meta:
template = 'django_tables2/bootstrap.html'
attrs = {'class': 'table table-bordered table-striped table-hover'}
sequence = ('selection','username', 'first_name', 'last_name', 'group')
呈现的表格中显示用户的群组。
表格如下:
peanut
感谢帮助
答案 0 :(得分:0)
我找到了解决方案:
class UserTable(tables.Table):
selection = tables.CheckBoxColumn(accessor="pk", attrs={"th__input": {"onclick": "toggleall(this)"}}, orderable=False)
username = tables.LinkColumn('edituser', args=[A('pk')])
groups = tables.Column(empty_values=())
class Meta:
model = User
exclude = ('id', 'password', 'is_active', 'is_staff','date_joined', 'last_login', 'is_superuser')
template = 'django_tables2/bootstrap.html'
attrs = {'class': 'table table-bordered table-striped table-hover'}
sequence = ('selection','username', 'first_name', 'last_name')
def render_groups(self, record):
if record.groups.all():
return ', '.join([group.name for group in record.groups.all()])
return '-'
render_groups返回用户所属的所有列表