title: Set initial value for dynamically created ModelChoiceField in Class Based View
body: <p>I'm creating a <code>Form</code> which has a <code>ModelChoiceField</code>. I'm able to set the values for this field as follows:</p>
  
  <pre><code>class DeviceForm(Form):
      devices = ModelChoiceField(queryset=None, widget=RadioSelect, empty_label=None)
  
      def __init__(self, *args, **kwargs):
          super(DeviceForm, self).__init__(*args, **kwargs)
          user_id = kwargs['initial']['user_id']
          devices = Device.objects.filter(user_id=user_id, is_validated=True)
          self.fields['devices'].queryset = devices
  </code></pre>
  
  <p>This works perfectly, but I can't seem to be able to set an initial value. I've tried multiple things such as adding an initial value to <code>get_initial()</code>: </p>
  
  <pre><code>def get_initial(self):
      initial = super(DeviceView, self).get_initial()
      initial['user_id'] = self.kwargs['user_id']
      default_device = Device.objects.get(user_id=self.kwargs['user_id'], is_validated=True, is_default=True)
      initial['devices'] = default_device.id
      return initial
  </code></pre>
  
  <p>But that doesn't work. I've come across multiple questions on StackOverflow which instantiate the form and pass in an initial value like so:</p>
  
  <pre><code>#Get default device
  form = DeviceForm(initial={'devices': default_device.id})
  </code></pre>
  
  <p>But ideally I would like to avoid it, since I'm trying to stick to the Django generic base views (which look like this, and don't allow for an <code>initial</code> kwarg as far as I'm aware):</p>
  
  <pre><code>def get(self, request, user_id, **kwargs):
      # Do stuff
  
      form = self.get_form()
      return self.render_to_response(self.get_context_data(form=form))
  </code></pre>
  
  <p>Setting devices in <code>get_initial()</code> should work, since the <code>self.get_form_kwargs()</code> returns:</p>
  
  <pre><code>{'prefix': None, 'initial': {'user_id': u'1', 'devices': 42}}
  </code></pre>
  
  <p>or, depending on what I'm supposed to choose:</p>
  
  <pre><code>{'prefix': None, 'initial': {'user_id': u'1', 'devices': &lt;Device: Device object&gt;}}
  </code></pre>
  
  <p>Doing the following still doesn't get it to work (basically the the same thing as <code>get_form()</code> manually).</p>
  
  <pre><code>def get(self, request, user_id, **kwargs):
      #Do stuff
      form_class = self.get_form_class()
      form = form_class(**self.get_form_kwargs())
      return self.render_to_response(self.get_context_data(form=form))
  </code></pre>
  
  <p>Setting the initial value in the constructor of the Form also doesn't work:</p>
  
  <pre><code>class DeviceForm(Form):
      devices = ModelChoiceField(queryset=None, widget=RadioSelect, empty_label=None)
  
      def __init__(self, *args, **kwargs):
          super(DeviceForm, self).__init__(*args, **kwargs)
          user_id = kwargs['initial']['user_id']
          devices = Device.objects.filter(user_id=user_id, is_validated=True)
          self.fields['devices'].queryset = devices
          default_device = devices.filter(is_default=True)
          self.fields['devices'].initial = default_device  # or default_device.id
  </code></pre>
  
  <p>Does anyone know of a way to accomplish this?</p>