django Initialising Choices in Form from Class Based View
django, django-class-based-views, forms
Solution
The view:
from django.views.generic import FormView
class EmailView(FormView):
# ...
def get_form_kwargs(self):
kwargs = super(EmailView, self).get_form_kwargs()
# get users, note: you can access request using: self.request
kwargs['users'] = users
return kwargs
The form:
from django import forms import Form
class EmailForm(Form):
users = MultipleChoiceField(required=False)
# ...
def __init__(self, *args, **kwargs):
self.users = kwargs.pop('users', None)
super(EmailForm, self).__init__(*args, **kwargs)
self.fields['users'].choices = self.users
Problem
I've seen a lot of answers about how this is solved when not using Class Based Views. Is there some really obvious thing I'm missing about doing it with CBVs? Basically I want to have a MultipleChoiceField in my form which has choices determined by what is happening in the view. e.g. I use the PK from the URL to do some backend requests and then those should be used to populate the choices. ``` # forms.py from django.forms import Form, MultipleChoiceField, CharField class EmailForm(Form): users = MultipleChoiceField(required=False) subject = CharField(max_length=100) message = CharField() def __init__(self, users=None, *args, **kwargs): super(EmailForm, self).__init__(*args, **kwargs) if users: self.fields['users'].choices = users #urls.py from django.conf.urls import url, patterns from .views import EmailView # url patterns urlpatterns = patterns('', url( r'^(?P<pk>\d+)$', EmailView.as_view(), name="maindex" ), ) #views.py from django.views.generic import FormView, TemplateView from .forms import EmailForm class EmailView(FormView): template_name = 'myapp/email.html' form_class = EmailForm success_ulr = '/thanks/' def form_valid(self, form): # Do stuff here return super(EmailView, self).form_valid(form) ``` Basically it boils down to how/where to call the init function from the view. How do I do that? Or is there another way I've missed? I thought of overriding `get_form_kwargs` in the view, but couldn't make that do anything. Thanks