Django-Admin: CharField as TextArea
For this case, the best option is probably just to use a TextField instead of CharField in your model. You can also override the formfield_for_dbfield
method of your ModelAdmin
class:
class CabAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
formfield = super(CabAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == 'descr':
formfield.widget = forms.Textarea(attrs=formfield.widget.attrs)
return formfield
Ayaz has pretty much spot on, except for a slight change(?!):
class MessageAdminForm(forms.ModelForm):
class Meta:
model = Message
widgets = {
'text': forms.Textarea(attrs={'cols': 80, 'rows': 20}),
}
class MessageAdmin(admin.ModelAdmin):
form = MessageAdminForm
admin.site.register(Message, MessageAdmin)
So, you don't need to redefine a field in the ModelForm to change it's widget, just set the widgets dict in Meta.
You will have to create a forms.ModelForm
that will describe how you want the descr
field to be displayed, and then tell admin.ModelAdmin
to use that form. For example:
from django import forms
class CabModelForm( forms.ModelForm ):
descr = forms.CharField( widget=forms.Textarea )
class Meta:
model = Cab
class Cab_Admin( admin.ModelAdmin ):
form = CabModelForm
The form
attribute of admin.ModelAdmin
is documented in the official Django documentation. Here is one place to look at.