Django Form: MultipleChoiceField and How To Have Choices From Model Example
Its been long time after my last post.
During my development in Django, I encountered this issue, where I don't know how to assign tuple value to choice argument in Django MultipleChoiceField.
So here I would like to share on how to do this.
Here are some information on the environment for this code:
Django 1.6.1
doc: https://docs.djangoproject.com/en/1.6/ref/forms/fields/#multiplechoicefield
Then looks into the forms.py:
#1 import the models which use for MultipleChoiceField
from TestChoice.models import Test
#2 create a static CHOICES variable to hole this tuple, you can perform any normal Django query here.
TEST_CHOICES = [[x.id, x.name] for x in Test.objects.all()]
#3 If you would like to add extra choices, then you can do the following:
TEST_CHOICES.insert(0, ['', "Empty"])
#4 The form field should looks like below, (required=False, only if you want this field to be optional)
class TestForm(forms.Form):
test= forms.MultipleChoiceField(choices=TEST_CHOICES, required=False)
Additional feature here is to change the widget of the multiple choice field. Default widget use by Django is SelectMultiple. To change the widget simple add the widget argument as shown below:
test= forms.MultipleChoiceField(choices=TEST_CHOICES, widget=forms.CheckboxSelectMultiple(), required=False)
The complete code should as below:
from TestChoice.models import Test
TEST_CHOICES = [[x.id, x.name] for x in Test.objects.all()]
class TestForm(forms.Form):
test = forms.MulitpleChoiceField(choices=TEST_CHOICES, widget=forms.CheckboxSelectMultiple(), requrired=False)
Your final result should looks like below:
Image above show the result when using widget SelectMultiple
Image above show the result when using widget CheckboxSelectMultiple
Hope this help.
Like usual, Happy Coding!!!
During my development in Django, I encountered this issue, where I don't know how to assign tuple value to choice argument in Django MultipleChoiceField.
So here I would like to share on how to do this.
Here are some information on the environment for this code:
Django 1.6.1
doc: https://docs.djangoproject.com/en/1.6/ref/forms/fields/#multiplechoicefield
Then looks into the forms.py:
#1 import the models which use for MultipleChoiceField
from TestChoice.models import Test
#2 create a static CHOICES variable to hole this tuple, you can perform any normal Django query here.
TEST_CHOICES = [[x.id, x.name] for x in Test.objects.all()]
#3 If you would like to add extra choices, then you can do the following:
TEST_CHOICES.insert(0, ['', "Empty"])
#4 The form field should looks like below, (required=False, only if you want this field to be optional)
class TestForm(forms.Form):
test= forms.MultipleChoiceField(choices=TEST_CHOICES, required=False)
Additional feature here is to change the widget of the multiple choice field. Default widget use by Django is SelectMultiple. To change the widget simple add the widget argument as shown below:
test= forms.MultipleChoiceField(choices=TEST_CHOICES, widget=forms.CheckboxSelectMultiple(), required=False)
The complete code should as below:
from TestChoice.models import Test
TEST_CHOICES = [[x.id, x.name] for x in Test.objects.all()]
class TestForm(forms.Form):
test = forms.MulitpleChoiceField(choices=TEST_CHOICES, widget=forms.CheckboxSelectMultiple(), requrired=False)
Your final result should looks like below:
Image above show the result when using widget SelectMultiple
Image above show the result when using widget CheckboxSelectMultiple
Hope this help.
Like usual, Happy Coding!!!
Comments
Post a Comment