I am building a Django app where employees submit their information through several related forms connected via one-to-one or one-to-many relationships.
In my models.py, I have an Employee model associated with other models. In forms.py I defined multiple forms such as EmployeeForm, ContactForm, HiringForm, PaymentForm, SystemUserForm, DocumentsForm, and ChildrenForm. Here is the content of forms.py:
class EmployeeForm(forms.ModelForm):
class Meta:
model = Employee
fields = '__all__'
widgets = {
'data_nascimento': forms.DateInput(attrs={'type': 'date'}),
'rg_expedicao': forms.DateInput(attrs={'type': 'date'}),
}
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = '__all__'
class HiringForm(forms.ModelForm):
class Meta:
model = Hiring
fields = '__all__'
widgets = {
'data_admissao': forms.DateInput(attrs={'type': 'date'}),
'hora_entrada': forms.TimeInput(attrs={'type': 'time'}),
'hora_saida': forms.TimeInput(attrs={'type': 'time'}),
}
class PaymentForm(forms.ModelForm):
class Meta:
model = Payment
fields = '__all__'
class SystemUserForm(forms.ModelForm):
class Meta:
model = SystemUser
fields = '__all__'
class DocumentsForm(forms.ModelForm):
class Meta:
model = Documents
fields = '__all__'
class ChildrenForm(forms.ModelForm):
class Meta:
model = Children
fields = '__all__'
widgets = {
'filho_nascimento': forms.DateInput(attrs={'type': 'date'}),
}
In my views.py, I process these forms as follows:
def employee_record(request):
if request.method == 'POST':
employee_form = EmployeeForm(request.POST)
contact_form = ContactForm(request.POST)
hiring_form = HiringForm(request.POST)
payment_form = PaymnentForm(request.POST)
system_user_form = SystemUserForm(request.POST)
documents_form = DocumentsForm(request.POST)
children_form = ChildrenForm(request.POST)
if employee_form.is_valid():
employee = employee_form.save()
if contact_form .is_valid() and hiring_form.is_valid() and payment_form_form.is_valid() and system_user_form.is_valid() and documents_form.is_valid() and children_form.is_valid():
contact = contact_form.save(commit=False)
hiring = hiring_form.save(commit=False)
payment = payment_form.save(commit=False)
system_user = system_user_form.save(commit=False)
documents = documents_form.save(commit=False)
children = children_form.save(commit=False)
contact.employee = employee
contact.save()
hiring.employee = employee
hiring.save()
payment.employee = employee
payment.save()
system_user.employee = employee
system_user.save()
documents.employee = employee
documents.save()
children.employee = employee
children.save()
return redirect('index')
[...]
The issue is that only employee_form is validating and saving data, whereas the other forms return a 'This field is required' error on the employee field. I suspect this is because the Employee object is being created on the fly and the related forms are not receiving it until after validation. What could be causing these validation errors, and how can I ensure all forms validate and save correctly?
Any guidance is greatly appreciated!