2

I have a multiple modelforms form multiple model. I want one single form for submitting all the values. It means I want one single createview for posting all modelforms data. I dont know how to that. forms.py:

class EmployeeAddModelForm(forms.ModelForm):
    """
    Creates a form for employee invitations
    """
    class Meta:
        model = Employee
        fields = [
            'e_id',
            'first_name',
            'last_name',
            'gender',
            'religion',
]
class WorkExperienceForm(forms.ModelForm):
    """
    Creates a form for saving employee work experiences
    """
    class Meta:
        model = WorkExperience
        fields = [
            'previous_company_name',
            'job_designation',
            'from_date',
            'to_date',
            'job_description',
        ]
class EducationForm(forms.ModelForm):
    """
    Creates a form for saving educational info of an employee
    """
    class Meta:
        model = Education
        fields = [
            'institution_name',
            'degree',
            'passing_year',
            'result',]
       

I have three model forms from three models in form.py. I want that my createview inherits all this modelforms and create a single form for posting data. views.py:

class EmployeeAddView(LoginRequiredMixin,CreateView):
    """
    Creates new employee
    """
    login_url = '/authentication/login/'
    template_name = 'employee/employee_add_form.html'
    form_class = EmployeeAddModelForm
    work_form_class = WorkExperienceForm
    queryset = Employee.objects.all()

    def form_valid(self, form):
        print(form.cleaned_data)
        return super().form_valid(form)

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        work_form = self.work_form_class(request.POST, prefix='work_form')
        print(form.errors)
        if form.is_valid() and work_form.is_valid():
            form.save(commit=True)
            work_form.save(commit=True)

            return redirect('employee:employee-list')
        return render(request, self.template_name, {'form': form, 'work_form': work_form})

    def get_success_url(self):
        return reverse('employee:employee-list')
fahim irfan
  • 73
  • 2
  • 16
  • 1
    Don't try to do this in the forms, do it in the view. – David Alford Jul 12 '20 at 17:38
  • How can I do that in view?? – fahim irfan Jul 12 '20 at 17:41
  • 1
    I believe this is what you are looking for: https://www.codementor.io/@lakshminp/handling-multiple-forms-on-the-same-page-in-django-fv89t2s3j If not, lmk – David Alford Jul 12 '20 at 17:43
  • I saw this. But this not clear to me. I use CBV for my views.py. what is wrong in my views.py? – fahim irfan Jul 12 '20 at 17:51
  • Possibly related: [python - How do I submit multiple forms with a single submit button in django? - Stack Overflow](https://stackoverflow.com/questions/15124567/how-do-i-submit-multiple-forms-with-a-single-submit-button-in-django); [python - django submit two different forms with one submit button - Stack Overflow](https://stackoverflow.com/questions/18489393/django-submit-two-different-forms-with-one-submit-button) – user202729 Aug 13 '21 at 11:58
  • @user202729 thanks. I have already solved it. – fahim irfan Aug 13 '21 at 15:56
  • @fahimirfan - I am trying to achieve something similar. Would you like to share how did you solve the issue? – Love Putin Not War Apr 09 '22 at 14:43
  • you can add multiple form class using prefix name. just like here I have work_form_class. you can add more like this. @user12379095 – fahim irfan Apr 10 '22 at 07:48

0 Answers0