[Answer]-How to add a modeladmin extra form field to readonly_fields?

1👍

What you need to do is write a read-only widget and specify it directly on your form, like this:

from ... import ReadOnlyWidget

class TaskForm(forms.ModelForm):
    extra_admin_field = forms.CharField(max_length=10)

    class Meta:
        model = Task
        widgets = {
            'extra_admin_field': ReadOnlyWidget,
        }

    def __init__(self, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        # set extra_admin_field value

There’s a readonly widget which should work (I haven’t tried it) here: http://djangosnippets.org/snippets/1682/

👤Greg

Leave a comment