title: Django class based views - UpdateView with two model forms - one submit
body: <p>I have a page with a list of users, and would like to be able to click a link to update their profile. When 'update' is clicked, I should be able to edit the username, first name, ... email, phone number, department etc, in a single page, using a single submit button. I accomplished this by using two forms, one for User, and one for the extra information. The ListView, DeleteView and CreateView work perfectly with these two forms, but not the UpdateView. I am not able to instantiate the two forms with initial data.</p>
  
  <p>The question is: how do I instantiate the two forms with data? Overwrite self.object? get_form_kwargs? What would be the most elegant solution?</p>
  
  <p>The UpdateView class is below. I am not looking for a 'copy-paste' solution, but maybe point me into the right direction.</p>
  
  <p>Thanks.</p>
  
  <p>Paul </p>
  
  <p>The phone number, department is defined in a model named Employee.</p>
  
  <pre><code>class Employee(models.Model):
      user = models.OneToOneField(User)
      phone_number = models.CharField(max_length=13, null=True)
      department = models.CharField(max_length=100)
  </code></pre>
  
  <p>The template is:</p>
  
  <pre><code>{% extends "baseadmin.html" %}
  {% load crispy_forms_tags %}
  
  {% block content %}
  &lt;h4&gt;Edit a user&lt;/h4&gt;
  &lt;form action="" method="post" class="form-horizontal"&gt;
      &lt;legend&gt;Edit a user&lt;/legend&gt;
      {% crispy form %}
      {% crispy form2 %}
      &lt;div class="form-actions"&gt;
          &lt;input type="submit" class="btn btn-primary" value="Save"&gt;
              &lt;a href="{% url 'client_list' %}" class="btn"&gt;Cancel&lt;/a&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  {% endblock content %}
  </code></pre>
  
  <p>The view class is:</p>
  
  <pre><code>class ClientUpdateView(UpdateView):
      model = User
      form_class = ClientsUserForm
      second_form_class = ClientsForm
      template_name = 'admin/client_update.html'
  
      def get_context_data(self, **kwargs):
          context = super(ClientUpdateView, self).get_context_data(**kwargs)
          context['active_client'] = True
          if 'form' not in context:
              context['form'] = self.form_class(self.request.GET)
          if 'form2' not in context:
              context['form2'] = self.second_form_class(self.request.GET)
          context['active_client'] = True
          return context
  
      def get(self, request, *args, **kwargs):
          super(ClientUpdateView, self).get(request, *args, **kwargs)
          form = self.form_class
          form2 = self.second_form_class
          return self.render_to_response(self.get_context_data(
              object=self.object, form=form, form2=form2))
  
      def post(self, request, *args, **kwargs):
          self.object = self.get_object()
          form = self.form_class(request.POST)
          form2 = self.second_form_class(request.POST)
  
          if form.is_valid() and form2.is_valid():
              userdata = form.save(commit=False)
              # used to set the password, but no longer necesarry
              userdata.save()
              employeedata = form2.save(commit=False)
              employeedata.user = userdata
              employeedata.save()
              messages.success(self.request, 'Settings saved successfully')
              return HttpResponseRedirect(self.get_success_url())
          else:
              return self.render_to_response(
                self.get_context_data(form=form, form2=form2))
  
      def get_success_url(self):
          return reverse('client_list')
  </code></pre>