0

I have a multiple modelforms form multiple model. I want one single CreateView for submitting all the values. I have three models(Employee, WorkExperience and Education). Models are connected using ForeignKey with each other.

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')
    education_form = self.education_form_class(request.POST, prefix='education_form')

    if form.is_valid() and work_form.is_valid():
        instance = form.save()
        work = work_form.save(commit=False)
        education = education_form.save(commit=False)
        work.employee = instance
        education.employee = instance
        work.save()
        education.save()

    if not education_form.is_valid():
        print("Education") 
        return redirect('employee:employee-list')

    def get_success_url(self):
        return reverse('employee:employee-list')

I am rendering two forms from my view class. But when I use 'work_form' in my template.html, nothing appears.

How can I render all modelforms in my view?

2
  • add work_form to get(self, request, *args, **kwargs) function or get_context_data(self, **kwargs) Commented Jul 13, 2020 at 7:40
  • can you give me a detail code? Commented Jul 13, 2020 at 7:52

1 Answer 1

1

override get function, because get request can not get work_form in default

def get(self, request, *args, **kwargs):
    form = self.form_class(**self.get_form_kwargs())
    work_form = self.work_form_class(prefix='work_form')
    return render(request, self.template_name, {'form': form, 'work_form': work_form})
Sign up to request clarification or add additional context in comments.

16 Comments

I have got another problem. Now it is not submitting data. @Blackdoor
plz ensure that the two forms should be in the same form tag block
Yes both are in same tag block. But I have to submit the form twice with same info. At first it is creating emlpoyee then it is creating work experience. But this needs to done in one submission
does work experience model has a foreignkey to employee model?
if so, post method has a bug that the relationship was not been created.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.