thedeepfield
thedeepfield

Reputation: 6196

DJANGO: Change field size of ModelMultipleChoiceField?

I was wondering how one changes the size of a ModelMultipleChoiceField? I've only seen examples of changing a CharField into a Textarea... Is this possible?

Models.PY

class FilterForm(forms.Form):
    Person = forms.ModelMultipleChoiceField(required=False, queryset=Person.objects.all(), attrs={'cols': 50, 'rows': 10})

Upvotes: 4

Views: 5426

Answers (3)

Anshik
Anshik

Reputation: 711

Try this:

from django.forms import widgets
class FilterForm(forms.Form):
    Person = forms.ModelMultipleChoiceField(required=False, queryset=Person.objects.all(), widget=widgets.SelectMultiple(attrs={'size': 20}))

Upvotes: 7

greg
greg

Reputation: 1121

Override the form's __init__ method and add:

self.fields['Person'].widget.attrs['size']='10'

Upvotes: 11

Abid A
Abid A

Reputation: 7858

You can change the width and height of the widget by applying CSS to the <select> element that Django creates.

Upvotes: 1

Related Questions