Why is my Django CreateView not able to create multiple records for work experience using formsets?

20 views Asked by At

I am currently working on a Django project where I'm implementing a Candidate Profile creation feature using the CreateView class. The Candidate Profile consists of general candidate information as well as multiple work experience records. To achieve this, I have created a form class for the candidate information (CandidateForm) and a formset for the work experience records (WorkExperienceFormSet).

Here's how I've set up my CandidateProfileCreateView:

class CandidateProfileCreateView(CreateView):
    form_class = CandidateForm
    model = Candidate_Account
    template_name = "candidate_create_profile.html"

    def get(self, request, *args, **kwargs):
        candidate_form = CandidateForm()
        workexperience_formset = WorkExperienceFormSet(queryset=WorkExperience.objects.none())
        
        return render(
            request,
            self.template_name,
            {
                'candidate_form': candidate_form,
                'workexperience_formset': workexperience_formset,
            }
        )

    def post(self, request, *args, **kwargs):
        candidate_form = CandidateForm(request.POST, request.FILES)
        workexperience_formset  = WorkExperienceFormSet(data=request.POST)

        if candidate_form.is_valid() and workexperience_formset .is_valid():
            candidate_object = candidate_form.save()
            workexperience_instances = workexperience_formset.save(commit=False)

            for workexperience_instance in workexperience_instances:
                workexperience_instance.candidate = candidate_object
                workexperience_instance.save()


            
            return HttpResponseRedirect(self.get_success_url(candidate_object))

        return render(
            request,
            self.template_name,
            {
                'candidate_form': candidate_form,
                'workexperience_formset': workexperience_formset,
            }
        )

    def get_success_url(self, candidate_object):
        return reverse('candidate_profile', kwargs={'pk': candidate_object.pk})

My problem is that although the candidate information is being saved correctly, I'm facing issues when trying to create multiple work experience records using the formset. The formset is not able to create or save the work experience instances as expected.

I've verified that both the candidate_form and workexperience_formset are valid in the post method, but the work experience records are not being saved to the database. I suspect there might be an issue with the loop where I'm assigning the candidate field to each work experience instance.

Could someone please review my code and help me understand why the work experience records are not being created and saved correctly? Am I missing something in the loop or the formset configuration that's causing this issue?

Any insights, suggestions, or solutions would be greatly appreciated. Thank you in advance!

0

There are 0 answers