def the_callable(obj):
return u'<a href="#">link from the callable for {0}</a>'.format(obj)
the_callable.allow_tags = True
class SomeAdmin(admin.ModelAdmin):
def the_method_in_modeladmin(self, obj):
return u'<a href="#">link from the method of modeladmin for {0}</a>'.format(obj)
the_method_in_modeladmin.allow_tags = True
readonly_fields = (the_callable, 'the_method_in_modeladmin', 'the_callable_on_object')
ObjModel.the_callable_on_object = lambda self, obj: u'<a href="#">link from the callable of the instance </a>'.format(obj)
ObjModel.the_callable_on_object.__func__.allow_tags = True
更新后的答案可以在this post中找到。
它使用
format_html
utility,因为allow_tags
已被弃用。而且ModelAdmin.readonly_fields的文档也非常有用。
我遵循了okm提供的链接,并设法在更改表单页面中包含了一个可点击的链接。
我的解决方案(添加到admin.ModelAdmin,而不是models.model)
老问题,但仍然值得回答。
Ref the doc,
readonly_fields
现在也支持这些自定义方式,工作原理与在注释中发布的the link相同:上述代码将在其更改表单页面中呈现三个只读字段。
相关问题 更多 >
编程相关推荐