Django admin How to display widget in readonly field

I want to display my widget in a field in admin django when the field is read-only.

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 the OK widget. But when I turn it on as readonly, I see a text string. (Django does not use my form, unless in read mode)

How can I make it use the form even in the readonly field?

or

How to display another widget if I set my field to be read-only?

+5
source share
2 answers

I would say that this is a feature.

When a field is set for reading, Django uses a display_for_fieldfunction that will adjust the result; you cannot configure it.

readonly formfield_for_dbfield, ModelAdmin .

+4

"disabled" , , get_form(...) :

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

, readonly self.readonly_fields, , self.fake_readonly_fields,

( formfield_for_dbfield) , , . , , , , , disabled.

: django modeladmin, readonly_fields

+3

All Articles