【发布时间】:2013-02-01 15:48:28
【问题描述】:
您可以在管理界面中为只读字段输出自定义 HTML 的 Django Docs state。这正是我需要的,但它似乎不起作用。
在 admin.py 中:
from django.contrib import admin
class ExampleAdmin(admin.ModelAdmin):
readonly_fields = ('myfield', )
def myfield(self, instance):
print 'This part of the code is never reached!'
return u'<b>My custom html for the readonly field!</b>'
myfield.allow_tags = True
admin.site.register(State, StateAdmin)
在models.py中:
class State(models.Model):
myfield = MyCustomField()
... etc ...
class MyCustomField(models.TextField):
def to_python(self, value):
... etc ...
该字段在管理员编辑页面上显示为只读。但是,应该创建自定义 html 的 'myfield' 方法从未被调用。
有人知道我做错了什么吗?
亲切的问候,
帕特里克
【问题讨论】:
标签: django django-admin