14

I want to display my widget on the field at django admin when the field is readonly.

admin.py

class AudioTrackAdminInline(admin.StackedInline):
    model = AudioTrack
    form = AudioTrackForm
    readonly_fields = ('file',)

forms.py

class AudioTrackForm(forms.ModelForm):
    class Meta:
        model = AudioTrack
        widgets = { 'file': MediaFileInput, } # my widget

When the file is not readonly, it displays widget OK. But when i include it as readonly, i see text line. (Django does not to use my form if readonly)

How can i get it to use form even at readonly field?

or

How to display another widget if i set my field readonly?

Feanor
  • 3,568
  • 5
  • 29
  • 49

2 Answers2

10

I would say it's a feature.

When field is set to readonly, Django uses display_for_field function which hardcodes the result, you can't customize it.

Another approach would be not to set the field as readonly and override formfield_for_dbfield, an undocumented ModelAdmin method and act accordingly to your needs.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
Łukasz
  • 35,061
  • 4
  • 33
  • 33
  • 1
    Overriding `formfield_for_dbfield` is dangerous since users can submit a `POST` and tamper fields you intended to be readonly (or just use the inspector to enable it). – WhyNotHugo Aug 08 '20 at 21:54
8

An alternative and more flexible solution is to set the "disabled" attribute of those fields you want to render in read-only mode, overriding the get_form(...) method of your admin class:

def get_form(self, *args, **kwargs):

    form = super(SupplierAdmin, self).get_form(*args, **kwargs)

    for field_name in self.fake_readonly_fields:
        form.base_fields[field_name].disabled = True

    return form

note that I take the name of readonly fields not from the usual self.readonly_fields but from a different array that I named self.fake_readonly_fields, in order to avoid name overlaps

The limit of the proposed solution (using formfield_for_dbfield) is that in that way you render at the same way all the database fields of a given type, wether it should be read-only or not. That way works if your entire form should be read-only, but if you deals with forms that are half read-only and half not, it is better to rely on the disabled attribute.

Further details on my answer here: django modeladmin, readonly_fields and boolean fields

Community
  • 1
  • 1
FSp
  • 1,545
  • 17
  • 37
  • This should use the `disabled` `Field` attribute, since not all widgets support the (HTML) attribute (e.g. with a `django.contrib.gis.forms.fields.PointField`), and it could be changed by the user! So `form.base_fields[field_name].disabled = True` should be used. – blueyed Nov 03 '16 at 17:25
  • In case of related fields (e.g. `ForeignKey` fields), you may want to remove the little buttons that add / edit / delete related objects. This can be done by setting the appropriate formfield widget attributes, as explained [here](https://stackoverflow.com/a/54775596/). – djvg Oct 09 '20 at 12:34