看一下:http ://docs.djangoproject.com/en/dev/ref/contrib/admin/,ModelAdmin.list_display 部分,它说:表示模型属性的字符串。这与可调用的行为几乎相同,但在这种情况下, self 是模型实例。这是一个完整的模型示例:
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
color_code = models.CharField(max_length=6)
def colored_name(self):
return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'colored_name')
所以我猜,如果你将这两个方法添加到 Person
def get_absolute_url(self):
return '/profiles/%s/' % (self.id)
def profile_link(self):
return '<a href="%s">%s</a>' % (self.get_absolute_url(), self.username)
profile_link.allow_tags = True
并将 PersonAdmin 更改为
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'colored_name', 'profile_link')
然后你完成了