http://docs.djangoproject.com/en/dev/ref/contrib/admin/、ModelAdmin.list_displayパーツを見てください、それは言います:モデルの属性を表す文字列。これは呼び出し可能オブジェクトとほぼ同じように動作しますが、このコンテキストでの自己はモデルインスタンスです。完全なモデルの例を次に示します。
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')
したがって、これら2つのメソッドを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')
その後、あなたはやった