Readonly method for field in Django admin interface is never called

django, django-admin

Solution

Looking at the "django/contrib/admin/util.py" file's `lookup_field` method, this appears to be the expected behavior. Here is the code you're using:

readonly_fields = ('myfield', )

Since `myfield` is an actual field defined in your model, having it in `readonly_fields` will only make it non-editable; it will not allow you to customize what gets displayed to the user. In order to do that, you have to give `readonly_fields` something that isn't an actual field, like `myfield_readonly`. You will then have to rename your `ModelAdmin`'s `myfield` method to `myfield_readonly`, of course, as well as the `myfield.allow_tags = True`. You'll probably also want to add `myfield_readonly.short_description = 'My Field'`. Lastly, you'll want to leave the actual `myfield` field out of the form using either `exclude` or `fields`.

Problem

The Django Docs state that you can output custom HTML for readonly fields in the admin interface. This is exactly what I need, but it does not seem to 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 is displayed as read-only on the admin edit page. However, the 'myfield' method that is supposed to create custom html is never called. Does anybody know what I'm doing wrong? Kind regards, Patrick

Original source