Hi there!
I am still new to Django but I have been trying to make an app where employees can submit their information.
Inside my models.py, I created a class named Employee, which is associated with other classes by establishing a one-to-one relationship or one-to-many relationship
My forms.py look like this:
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'}),
}
And on views.py, I have the following function:
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')
[...]
Right now, employee_form is the only one being validated, and the only form whose data is being saved on my database.
The other forms exhibit the error “This field is required”, which seems to be associated with ‘employee’.
Previous testing showed that only previously saved employees
Is this happening because the Employee object was just created, and the other forms/classes still can’t access it?
Sorry, this is probably a stupid question, but I have been stuck on this issue for a while, so anything would help.
Thanks in advance!