默认情况下,列表显示的第一列将链接到管理员编辑页面。如果您希望另一列或多列成为该链接,一种非常简单的方法如下:
class FooModelAdmin(admin.ModelAdmin):
list_display = ('foo_link', 'bar', 'another_bar', )
list_display_links = ('foo_link', 'another_bar', )
如果foo_link
不是模型的属性,它应该是一个可调用的,如下所示:
class FooModelAdmin(admin.ModelAdmin):
list_display = ('foo_link', 'bar', 'another_bar', )
list_display_links = ('foo_link', 'another_bar', )
def foo_link(self, obj):
return "%s blah blah" % obj.some_property # or anything you prefer e.g. an edit button
我的项目中的一个完整示例:
class SchoolTeacherAdmin(admin.ModelAdmin):
list_display = ('name', 'designation', 'school_name', 'school_code', 'date_of_birth', 'mobile', 'nid', 'edit', )
list_display_links = ('edit', )
def school_code(self, obj):
return obj.school.code
def school_name(self, obj):
return obj.school.name.upper()
def edit(self, obj):
return "Edit"