众所周知,ManytoMany关系不能列在list_display中。是否有任何工作可以使它像group1,group2等?
答案 0 :(得分:8)
bonus:将组显示为用户过滤器:
class SBUserAdmin(UserAdmin):
list_filter = UserAdmin.list_filter + ('groups__name',)
list_display = ('username','custom_group', )
def custom_group(self, obj):
"""
get group, separate by comma, and display empty string if user has no group
"""
return ','.join([g.name for g in obj.groups.all()]) if obj.groups.count() else ''
admin.site.unregister(User)
admin.site.register(User, SBUserAdmin)
答案 1 :(得分:5)
我不理解您的示例(group1,group2),但您当然可以将任何函数用作更改列表视图中的列,这意味着您可以执行您想要显示的内容!
示例:
class MyModelAdmin(admin.ModelAdmin):
list_display = ('foo', 'bar')
def foo(self):
return "This column is Foo"
def bar(self, obj):
try:
return obj.m2m.latest('id')
except obj.DoesNotExist:
return "n/a"
# there's a few more things you can do to customize this output
def bar(self, obj):
return '<span style="color:red;">By the way, I am red.</span>'
bar.short_description = "My New Column Label"
bar.allow_tags = True