How can i set the size of rows , columns in textField in Django Models

I have this Model

summary         = models.TextField()

But I want to have only 4 rows and 15 columns.

Also if I do that do I need to install database again or not.


TextField is a type of field, which when used by a ModelForm by default uses the Textarea widget. Fields deal with backend storage, widgets with front-end editing.

So you need to specify the widget you want to go with your field. You want to create a ModelForm and specify the widget you want to use, per the documentation:

from django import forms

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        widgets = {
          'summary': forms.Textarea(attrs={'rows':4, 'cols':15}),
        }

Here is the form specific equivalent for changing the rows/cols attribute to a textarea. Use the forms.Textarea widget and set attrs dictionary values:

class LocationForm(forms.ModelForm):

    auto_email_list = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}))
    notes = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 4, 'cols': 40}))

    class Meta:
        model = Location
        exclude = ['id']

Hope this helps those looking for form specific


If you're using django-crispy-forms, in your __init__() function you can access the widget attrs like so:

self.fields['summary'].widget.attrs['rows'] = 4
self.fields['summary'].widget.attrs['columns'] = 15

I suppose you could set the attrs this way it in a non crispy-form ModelForm, but you may as well do it in your widget definition, as shown above. I just found this post and thought I'd share how I did it with crispyforms.