【发布时间】:2014-08-05 23:34:51
【问题描述】:
我需要将带有指向我网站上模型视图的链接的字段添加到 django 管理视图。
当我将字段名称添加到 list_display 并定义呈现此 url 的方法时:
class SetAdmin(admin.ModelAdmin):
list_display = ['many other fields', 'show_set_url']
def show_set_url(self, obj):
return '<a href="#">Set</a>' # render depends on other fields
它显示在 django admin 的 Sets 列表中,但不以模型形式显示。 如何解决这个问题并在 django admin 中添加指向 Sets Form 的链接?
我也尝试为这个模型创建自定义表单:
from core.models import Set
from django import forms
class SetAdminForm(forms.Form):
class Meta:
model = Set
def __init__(self, *args, **kwargs):
super(SetAdminForm, self).__init__(*args, **kwargs)
self.fields['foo'] = forms.IntegerField(label=u"Link")
但现在形式上有明显的效果。
【问题讨论】:
-
不,现在我得到 KeyError: "Key u'show_set_url' not found in Form"