Django class based views - UpdateView with two model forms - one submit -


i have page list of users, , able click link update profile. when 'update' clicked, should able edit username, first name, ... email, phone number, department etc, in single page, using single submit button. accomplished using 2 forms, 1 user, , 1 information. listview, deleteview , createview work these 2 forms, not updateview. not able instantiate 2 forms initial data.

the question is: how instantiate 2 forms data? overwrite self.object? get_form_kwargs? elegant solution?

the updateview class below. not looking 'copy-paste' solution, maybe point me right direction.

thanks.

paul

the phone number, department defined in model named employee.

class employee(models.model):     user = models.onetoonefield(user)     phone_number = models.charfield(max_length=13, null=true)     department = models.charfield(max_length=100) 

the template is:

{% extends "baseadmin.html" %} {% load crispy_forms_tags %}  {% block content %} <h4>edit user</h4> <form action="" method="post" class="form-horizontal">     <legend>edit user</legend>     {% crispy form %}     {% crispy form2 %}     <div class="form-actions">         <input type="submit" class="btn btn-primary" value="save">             <a href="{% url 'client_list' %}" class="btn">cancel</a>     </div> </form> {% endblock content %} 

the view class is:

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() , form2.is_valid():             userdata = form.save(commit=false)             # used set password, 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') 

you should able use "instance" kwarg instantiation of form existing model if that's need. example:

context['form'] = self.form_class(self.request.get, instance=request.user) 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -