Django — Override One Specific Field Widget in Admin


formfield_overrides is just too global and redefining form fields has many drawbacks (loss of original field arguments for example and ModelForm magic). What do we do? I keep forgetting about the ModelForm meta class widget attribute.

The ModelForm meta class takes a widget attribute which is a map of fieldnames to widgets:

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        widgets = {
            'activity': forms.Textarea(attrs={'disabled': True}),
            'log': forms.Textarea(attrs={'disabled': True}),

class MyAdmin(admin.ModelAdmin):
    form = OrderForm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s