The readonly method for a field in the Django admin interface is never called

The state of the Django Docs that you can display custom HTML for readonly fields in the admin interface. This is exactly what I need, but it does not work.

In 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)

In models.py:

class State(models.Model):
    myfield = MyCustomField()
    ... etc ...

class MyCustomField(models.TextField):
    def to_python(self, value):
        ... etc ...

The field appears as read-only on the admin edit page. However, the myfield method, which should create custom html, is never called.

Does anyone know what I'm doing wrong?

Yours faithfully,

Patrick

+5
source share
1 answer

Looking at the django / contrib / admin / util.py file method lookup_field, it looks like the expected behavior. Here is the code you use:

readonly_fields = ('myfield', )

myfield - , , readonly_fields ; , . , readonly_fields , , myfield_readonly. ModelAdmin myfield myfield_readonly, myfield.allow_tags = True. , myfield_readonly.short_description = 'My Field'. , myfield , exclude, fields.

+11

All Articles