[ACCEPTED]-Increase CharField Width in Django Forms-django-forms

Accepted answer
Score: 22

Try specifying widgets in the form, as in 2 the following example:

models.py

class Photo(models.Model):
    caption = models.CharField(max_length=80)

admin.py

class PhotoAdmin(admin.ModelAdmin):
    list_display = (u'preview', u'caption')
    search_fields = (u'caption')
    form = forms.PhotoAdminForm

forms.py

class PhotoAdminForm(forms.ModelForm):
    class Meta:
        widgets = { 'caption': forms.TextInput(attrs={'size': 80})}

As alternative, you 1 can use the following:

forms.py

class PhotoAdminForm(forms.ModelForm):
    caption = forms.CharField(widget=forms.TextInput(attrs={'size':80}))
Score: 4

Since 1.7 you can solve it like this:

admin.py

class PhotoAdmin(admin.ModelAdmin):
    list_display = (u'preview', u'caption')
    search_fields = (u'caption')
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size': '80'})}
    }

0

More Related questions