2016-12-24 3 views
-1

デフォルトのdjangoモデル(ユーザーとグループ)を使用していて、django tables2でレンダリングされたテーブルにユーザーのグループを表示しようとしています。次のようにユーザーのグループをdjangoのテーブルに表示する

の表は、次のとおりです。私は解決策を見つけることができました

答えて

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')]) 
    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がユーザーとなっているすべてのリストを返します

の部分
関連する問題