# Working with different forms but validation only works for one

**URL:** <https://forum.djangoproject.com/t/working-with-different-forms-but-validation-only-works-for-one/38740>\
**Category:** Forms & APIs\
**Created:** [February 10, 2025, 7:22pm UTC](https://forum.djangoproject.com/t/working-with-different-forms-but-validation-only-works-for-one/38740 "2025-02-10T19:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deborah\_HM](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/deborah_hm/32/26832_2.png) [@Deborah\_HM](https://forum.djangoproject.com/u/Deborah_HM)\
**Post date:** [February 10, 2025, 7:22pm UTC](https://forum.djangoproject.com/t/working-with-different-forms-but-validation-only-works-for-one/38740/1 "2025-02-10T19:22:20Z")

</div>

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

 ![models](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/8/8/8896f7a7beb253750ee79c1139e905da50efc550.png)

My forms.py look like this:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [February 10, 2025, 7:29pm UTC](https://forum.djangoproject.com/t/working-with-different-forms-but-validation-only-works-for-one/38740/2 "2025-02-10T19:29:04Z")

</div>

The three backticks before and after your code have come out as curly quotes, so the code formatting isn’t working.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 10, 2025, 7:30pm UTC](https://forum.djangoproject.com/t/working-with-different-forms-but-validation-only-works-for-one/38740/3 "2025-02-10T19:30:08Z")

</div>

Welcome @Deborah_HM !

See the docs for [Prefixes for forms](https://docs.djangoproject.com/en/5.1/ref/forms/api/#prefixes-for-forms)

---

<div class="post-metadata">

**Author:** ![Deborah\_HM](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/deborah_hm/32/26832_2.png) [@Deborah\_HM](https://forum.djangoproject.com/u/Deborah_HM)\
**Post date:** [February 17, 2025, 3:49pm UTC](https://forum.djangoproject.com/t/working-with-different-forms-but-validation-only-works-for-one/38740/4 "2025-02-17T15:49:45Z")

</div>

Thank you for your responses. I adjusted the backtick and I think the code formatting should be working now.

I included the prefixes in my views.py like this:

```auto
if request.method == 'POST':
        employee_form = EmployeeForm(request.POST, prefix='employee')
        contact_form = ContactForm(request.POST, prefix='contact')
        hiring_form = HiringForm(request.POST, prefix='hiring')
        payment_form = PaymnentForm(request.POST, prefix='payment')
        system_user_form = SystemUserForm(request.POST, prefix='system')

[...]

```

I also made some changes to my forms.py which no longer uses all fields:

```auto
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
        exclude = ['employee']
        
class HiringForm(forms.ModelForm):
    class Meta:
        model = Hiring
        exclude = ['employee']
        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
        exclude = ['employee']

class SystemUserForm(forms.ModelForm):
    class Meta:
        model = SystemUser
        exclude = ['employee']

```

From what I read, excluding ‘employee’ in this case would prevent issues with the one-to-one relationship these forms have; is this correct?  
I also noticed my DocumentsForm and ChildrenForm (which I had previously included) were causing errors due to the fact that they allow file uploads.  
I have removed them for the time being and will learn how to deal with them properly. Apart from that, my code seems to be running properly now, and the data from all forms are being saved to django admin and to my postgres database, so thank you for your help!
