drewman
drewman

Reputation: 1595

Is it possible to override a single textfield in Django admin?

I was looking to override only a single TextField in the Django admin but not have it influence the other textfield, which I would like to be the default. Here is what I have so far.

formfields_overrides={
    models.TextField: {'widget': Textarea(attrs={'rows':1, 'cols':40})}
}

Upvotes: 2

Views: 774

Answers (1)

Ismail Badawi
Ismail Badawi

Reputation: 37177

You can specify a form to be used on the add/change pages. By default this is a plain old ModelForm.

Say your model is called MyModel, and you want to change the widget for field1 but not for field2:

class MyModelForm(forms.ModelForm):
    class Meta:
         model = MyModel
         widgets = {
             'field1': Textarea(attrs={'rows':1, 'cols':40}),
         }

...
class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

Upvotes: 3

Related Questions