[Solved]-Django class based views – UpdateView with two model forms – one submit

7👍

You should be able to use the “instance” kwarg for the instantiation of the form from an existing model if that’s what you need. Example:

context['form'] = self.form_class(self.request.GET, instance=request.user)
👤kdb

1👍

Try to quit the self.request.get on get_content_data forms constructors
and use the “object” variable. With that get you reinitialize the constructor, more in this link.

My code:

 context['form'] = self.form_class(instance=self.object)

1👍

Get rid of the method get, u don’t need to superscribe it. And apply the following changes in place:

class ClientUpdateView(UpdateView):
       model = Employee # You can access the user by the profile, but not the 
       # oposit 
       form_class = ClientsForm
       second_form_class = ClientsUserForm
       template_name = 'admin/client_update.html'
      
       # def get(self, request, *args, **kwargs):
       # get rid off

       def get_context_data(self, **kwargs):
           kwargs['active_client'] = True
           if 'form' not in kwargs:
              kwargs['form'] = self.form_class(instance=self.get_object())
           if 'form2' not in kwargs:
              kwargst['form2'] = \ 
          self.second_form_class(instance=self.get_object().user)
          return super(ClientUpdateView, self).get_context_data(**kwargs)

       def post(self, request, *args, **kwargs): 
           ...

Leave a comment