Textarea in DJango admin form

For CharField Django admin automatically generates input html block. But if we need to make a CharField appear as textarea in html we can do so by creating a Form class for the model. Consider the following model:

file: models.py

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=500)

Next we create the form & add it to ModelAdmin subclass

file: admin.py

from models import MyModel
from django.contrib import admin
from django import forms

class MyModelForm(forms.ModelForm ):
    details = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

admin.site.register(MyModel, MyModelAdmin)

Leave a Reply