Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readonly method for field in Django admin interface is never called

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

like image 389
user1349674 Avatar asked Feb 16 '13 21:02

user1349674


People also ask

How do I add a readonly field in Django admin?

Django admin by default shows all fields as editable and this fields option will display its data as-is and non-editable. we use readonly_fields is used without defining explicit ordering through ModelAdmin or ModelAdmin. fieldsets they will be added. Now we make the Question text fields read-only.

How do you make a field read-only in Django?

Setting readonly on a widget only makes the input in the browser read-only. Adding a clean_sku which returns instance. sku ensures the field value will not change on form level. This way you can use model's (unmodified save) and avoid getting the field required error.

What is the purpose of the admin site in a django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

What is list_ display in Django?

It provides a simple UI for creating, editing and deleting data defined with the Django ORM. In this article we are going to enable the admin user interface for a simple model and customize it from a simple list view to a more user friendly table like interface.


1 Answers

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.

like image 112
Adam Taylor Avatar answered Sep 30 '22 10:09

Adam Taylor