django管理中的自定义html字段changelist_view
我想使用 django admin 进行一些小的自定义 - 特别是 changelist_view
class FeatureAdmin(admin.ModelAdmin):
list_display = (
'content_object_change_url',
'content_object',
'content_type',
'active',
'ordering',
'is_published',
)
list_editable = (
'active',
'ordering',
)
list_display_links = (
'content_object_change_url',
)
admin.site.register(get_model('features', 'feature'), FeatureAdmin)
这个想法是 'content_object_change_url'
可以是到另一个对象的 change_view 的链接
...方便管理员用户快速直接导航到该项目。
对于此类事情,我的另一种情况是添加外部源的链接或图像字段的缩略图。
我原以为我听说过“插入 html”选项——但也许我有点言过其实了。
感谢您的帮助!
I'd like to some little customisation with the django admin -- particularly the changelist_view
class FeatureAdmin(admin.ModelAdmin):
list_display = (
'content_object_change_url',
'content_object',
'content_type',
'active',
'ordering',
'is_published',
)
list_editable = (
'active',
'ordering',
)
list_display_links = (
'content_object_change_url',
)
admin.site.register(get_model('features', 'feature'), FeatureAdmin)
The idea is that the 'content_object_change_url'
could be a link to another object's change_view
... a convenience for the admin user to quickly navigate directly to the item.
The other case I'd have for this kind of thing is adding links to external sources, or thumbnails of image fields.
I had thought I'd heard of a 'insert html' option -- but maybe I'm getting ahead of myself.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在
FeatureAdmin
类上提供一个自定义方法,该方法返回content_object_change_url
的 HTML:请参阅 文档。
You can provide a custom method on the
FeatureAdmin
class which returns HTML forcontent_object_change_url
:See the documentation.
注意并使用 format_html (请参阅文档此处)作为 mark_safe util 已自 1.10 版本起已弃用。此外,自版本 1.11 起,将删除对 ModelAdmin 方法上的 allowed_tags 属性的支持。
Pay attention and use format_html (See docs here) as the mark_safe util has been deprecated since version 1.10. Moreover, support for the allow_tags attribute on ModelAdmin methods will be removed since version 1.11.
我花了两个小时才找出为什么丹尼尔·罗斯曼的解决方案对我不起作用。尽管他是对的,但有一个例外:当您想在
Admin
中创建自定义计算字段(只读)时。这行不通。非常简单的解决方案(但很难找到)是在特殊的构造函数中返回字符串:SafeText()
。也许这与 Django 2 或readonly_fields
(其行为与经典字段不同)相关。下面是一个工作示例,可以工作,但如果没有
SafeText()
则无法实现:It took me two hours to find out why Daniel Roseman's solution doesn't work for me. Even though he is right, there's one exception: when you want to make custom calculated fields (read only) in the
Admin
. This wont work. The very easy solution (but hard to find) is to return your string in a special constructor:SafeText()
. Maybe this is linked with Django 2 or withreadonly_fields
(which behave differently from classical fields)Here's a working sample that works but doesn't without
SafeText()
: