Auswahl im MultipleChoiceField vorbelegen

7 views
Skip to first unread message

Stephan

unread,
Jan 19, 2012, 9:30:28 AM1/19/12
to Django-de
Hallo,

ich habe ein MultipleChoiceField und möchte da je nach Datensatz
verschiedene Einträge als ausgewählt vorbelegen.

variant =
forms.ModelMultipleChoiceField(queryset=Variant.objects.all(),
label='Varianten', required=False,

widget=forms.SelectMultiple(attrs={'class':"file_upload_area", }))

Geht so etwas überhaupt? In der Doku habe ich nichts finden können.

Grüße
Stephan

ses

unread,
Jan 20, 2012, 5:35:22 AM1/20/12
to Django-de
Moin,

du findest das alles in der Dokumentation[1] ;-)

Sonnige Grüße,
Sebastian

[1]: <https://docs.djangoproject.com/en/1.3/ref/forms/api/#dynamic-
initial-values>

Martin Mahner

unread,
Jan 30, 2012, 8:12:36 AM1/30/12
to Django-de
Du kannst initial Werte als eine Liste übergeben.

>>> choices = (('a', 'AAA'), ('b', 'BBB'), ('c', 'CCC'))
>>> initial = {'variants': ('a', 'b')}
>>> class F(forms.Form):
variants = forms.MultipleChoiceField(choices=choices)

>>> F(initial=initial).as_ul()

<li>
<label for="id_variants">Variants:</label>
<select multiple="multiple" name="variants" id="id_variants">
<option value="a" selected="selected">AAA</option>
<option value="b" selected="selected">BBB</option>\
<option value="c">CCC</option>
</select>
</li>
Reply all
Reply to author
Forward
0 new messages