Django Docs state您可以在管理界面中爲只讀字段輸出自定義HTML。這正是我需要的,但它似乎並不奏效。Django管理界面中字段的只讀方法從不稱爲
在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'方法從不會被調用。
有人知道我在做什麼錯嗎?
親切的問候,
帕特里克