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
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
                        Basically, what I've done in a similar case is the following (Python 3.5, Django 1.8):
def get_form(self, *args, **kwargs):
    form= super().get_form(*args, **kwargs)
    form.fields['rank'].choices= <sequence of 2-tuples>
    return form
where obviously rank is the field name. This way I use the default form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With