I have a django UpdateView which needs to inherit three different models and different models. CreateView is working fine with three different modelforms.
models.py:
class Employee(models.Model): """ Create employee attributes """ employee_user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) e_id = models.IntegerField(unique=True, null=True, blank=True) first_name = models.CharField(max_length=128,blank=True) last_name = models.CharField(max_length=128, blank=True) ..... class WorkExperience(models.Model): """ Stores employee previous work experiences """ employee = models.ForeignKey('Employee', related_name='we_employee', on_delete=models.CASCADE, null=True) previous_company_name = models.CharField(max_length=128, null=True) job_designation = models.CharField(max_length=128, null=True) from_date = models.DateField(null=True) ...... class Education(models.Model): """ Stores employee education background """ employee = models.ForeignKey('Employee', related_name='edu_employee', on_delete=models.CASCADE, null=True) institution_name = models.CharField(max_length=128, null=True) degree = models.CharField(max_length=128, null=True) .....
views.py:
class EmployeeUpdateView(LoginRequiredMixin,UpdateView): """ Update a created a employee """ login_url = '/authentication/login/' template_name = 'employee/employee_add_form.html' form_class = EmployeeAddModelForm work_form_class = WorkExperienceForm education_form_class = EducationForm queryset = Employee.objects.all() def form_valid(self, form): print(form.cleaned_data) return super().form_valid(form) def get_object(self): id_ = self.kwargs.get("id") return get_object_or_404(Employee, id=id_)
WHen I go to Update view, I am only able to update EmployeeAddModelForm values. But other form’s(WorkExperienceForm, EducationForm ) fields do not appear let alone edit the values of the fields. I suppose my views.py is not correct. I need suggestion to correct my updateview.
Advertisement
Answer
If you want to put multiple forms in one HTML file, in my way, write as follows.
class EmployeeUpdateView(LoginRequiredMixin,UpdateView): login_url = '/authentication/login/' def form_valid(self, form): print(form.cleaned_data) return super().form_valid(form) def get(self, request, *args, **kwargs): id_ = self.kwargs.get("id") user = Employee.objects.get(id=id_) work = WorkExperience.objects.get(employee=user) education = Education.objects.get(employee=user) return render(request, self.template_name, { 'form': EmployeeAddModelForm(instance=user), 'work_form': WorkExperienceForm(instance=work), 'education_form': EducationForm(instance=education) } ) def post(self, request): first_name = request.POST['first_name'] # Your own code... .....
I think the argument of each form depends on the definition of Form.
And in a very simple way, the HTML file looks like this:
<body> <form method="post"> {% csrf_token %} {{ employ_form }} {{ work_form }} {{ edu_form }} <button type="submit">OK</button> </form? </body> </html>
I’m not sure if this is the best option.