擅长:python、mysql、java
<p>老问题,但仍然值得回答。</p>
<p><a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields" rel="noreferrer">Ref the doc</a>,
<code>readonly_fields</code>现在也支持这些自定义方式,工作原理与在注释中发布的<a href="https://stackoverflow.com/questions/1949248/how-to-add-clickable-links-to-a-field-in-django-admin">the link</a>相同:</p>
<pre><code>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
</code></pre>
<p>上述代码将在其更改表单页面中呈现三个只读字段。</p>