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:
JavaScript
x
28
28
1
class Employee(models.Model):
2
"""
3
Create employee attributes
4
"""
5
employee_user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
6
e_id = models.IntegerField(unique=True, null=True, blank=True)
7
first_name = models.CharField(max_length=128,blank=True)
8
last_name = models.CharField(max_length=128, blank=True)
9
..
10
class WorkExperience(models.Model):
11
"""
12
Stores employee previous work experiences
13
"""
14
employee = models.ForeignKey('Employee', related_name='we_employee', on_delete=models.CASCADE, null=True)
15
previous_company_name = models.CharField(max_length=128, null=True)
16
job_designation = models.CharField(max_length=128, null=True)
17
from_date = models.DateField(null=True)
18
19
20
class Education(models.Model):
21
"""
22
Stores employee education background
23
"""
24
employee = models.ForeignKey('Employee', related_name='edu_employee', on_delete=models.CASCADE, null=True)
25
institution_name = models.CharField(max_length=128, null=True)
26
degree = models.CharField(max_length=128, null=True)
27
..
28
views.py:
JavaScript
1
19
19
1
class EmployeeUpdateView(LoginRequiredMixin,UpdateView):
2
"""
3
Update a created a employee
4
"""
5
login_url = '/authentication/login/'
6
template_name = 'employee/employee_add_form.html'
7
form_class = EmployeeAddModelForm
8
work_form_class = WorkExperienceForm
9
education_form_class = EducationForm
10
queryset = Employee.objects.all()
11
12
def form_valid(self, form):
13
print(form.cleaned_data)
14
return super().form_valid(form)
15
16
def get_object(self):
17
id_ = self.kwargs.get("id")
18
return get_object_or_404(Employee, id=id_)
19
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.
JavaScript
1
28
28
1
class EmployeeUpdateView(LoginRequiredMixin,UpdateView):
2
login_url = '/authentication/login/'
3
4
def form_valid(self, form):
5
print(form.cleaned_data)
6
return super().form_valid(form)
7
8
def get(self, request, *args, **kwargs):
9
id_ = self.kwargs.get("id")
10
user = Employee.objects.get(id=id_)
11
work = WorkExperience.objects.get(employee=user)
12
education = Education.objects.get(employee=user)
13
14
15
return render(request, self.template_name, {
16
'form': EmployeeAddModelForm(instance=user),
17
'work_form': WorkExperienceForm(instance=work),
18
'education_form': EducationForm(instance=education)
19
}
20
)
21
22
23
def post(self, request):
24
25
first_name = request.POST['first_name']
26
# Your own code...
27
..
28
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:
JavaScript
1
11
11
1
<body>
2
<form method="post">
3
{% csrf_token %}
4
{{ employ_form }}
5
{{ work_form }}
6
{{ edu_form }}
7
<button type="submit">OK</button>
8
</form?
9
</body>
10
</html>
11
I’m not sure if this is the best option.