Why not use ModelMultipleChoiceField instead?
You can do it simply:
class NavigatorExportForm(forms.Form): languages = forms.ModelMultipleChoiceField(queryset=Language.objects.all()) def __init__(self, app_id, *args, **kwargs): super(NavigatorExportForm, self).__init__(*args, **kwargs)
Thus, you not only restrict access to the widget, but also in the field (which gives you confirmation of the data).
Using this method, the displayed string in the widget will be the result of the __unicode__ method of the __unicode__ object. If this is not what you want, you can write the following custom field, as described in the ModelChoiceField link :
class LanguageMultipleChoiceField(forms.ModelMultipleChoiceField): def label_from_instance(self, obj): return obj.language_code
and use this class instead of ModelMultipleChoiceField in your form.
source share