擅长:python、mysql、java
<p>更新后的答案可以在<a href="https://stackoverflow.com/a/31745953/2996101">this post</a>中找到。</p>
<p>它使用<code>format_html</code><a href="https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.html.format_html" rel="nofollow noreferrer">utility</a>,因为<code>allow_tags</code>已被弃用。</p>
<p>而且<a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields" rel="nofollow noreferrer">ModelAdmin.readonly_fields</a>的文档也非常有用。</p>
<pre><code>from django.utils.html import format_html
from django.contrib import admin
class SomeAdmin(admin.ModelAdmin):
readonly_fields = ('my_clickable_link',)
def my_clickable_link(self, instance):
return format_html(
'<a href="{0}" target="_blank">{1}</a>',
instance.<link-field>,
instance.<link-field>,
)
my_clickable_link.short_description = "Click Me"
</code></pre>