在 django tables2 中显示用户组

Show user's group in django tables2

我正在使用默认的 django 模型(用户和组)并且我正在尝试在使用 django tables2.

呈现的 table 中显示用户的组

table如下:

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')

感谢帮助

我找到了解决方案:

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 return 用户所属的所有列表