Dynamic choices for Django SelectMultiple Widget

Posted by PhilGo20 on Stack Overflow See other posts from Stack Overflow or by PhilGo20
Published on 2010-04-12T16:06:45Z Indexed on 2010/04/12 16:43 UTC
Read the original article Hit count: 329

Filed under:
|
|

I'm building a form (not modelForm) where i'd like to use the SelectMultiple Widget to display choices based on a query done during the init of the form.

I can think of a few way to do this but I am not exactly clear on the right way to do it. I see different options.

I get the "choices" I should pass to the widget in the form init but I am not sure how I should pass them.

class NavigatorExportForm(forms.Form):

def __init__(self,user, app_id, *args,**kwargs):
    super (NavigatorExportForm,self ).__init__(*args,**kwargs) # populates the form
    language_choices = Navigator.admin_objects.get(id=app_id).languages.all().values_list('language', flat=True)

languages = forms.CharField(max_length=2, widget=forms.SelectMultiple(choices=???language_choices))

© Stack Overflow or respective owner

Related posts about django

Related posts about forms