4

I am getting the above error when I have tried to convert my inline_formset to have at least the first row required. (please see here for the StackOverflow question)

My existing code is below:

#views.py
def application(request, job_id):
    job = get_object_or_404(Job, pk=job_id)
    #return 404 if job isn't yet published
    if (job.pub_date>timezone.now() or job.close_date<timezone.now()):
        return HttpResponseNotFound('<h1>Job not found</h1>')

    #create all the inlineformsets (can_delete) set to false as will always be empty upon population
    EducationInlineFormSet = inlineformset_factory(Applicant, Education, extra=1, can_delete=False)
    QualificationInlineFormSet = inlineformset_factory(Applicant, Qualification, extra=1, can_delete=False)
    EmploymentInlineFormSet = inlineformset_factory(Applicant, Employment, extra=1, can_delete=False)

    if request.method == 'POST':
        applicant = Applicant(job=job)
        form = ApplicantForm(request.POST, instance=applicant)
        bottom_form = ApplicantFormBottom(request.POST, instance=applicant)
        education_formset = EducationInlineFormSet(request.POST, instance=applicant)
        qualification_formset = QualificationInlineFormSet(request.POST, instance=applicant)
        employment_formset = EmploymentInlineFormSet(request.POST, instance=applicant)

        #check all of the forms and formsets are valid
        if form.is_valid() and bottom_form.is_valid() and education_formset.is_valid() and qualification_formset.is_valid() and employment_formset.is_valid():
            # save the model to database, directly from the form:
            form.save()
            bottom_form.save()
            education_formset.save()
            qualification_formset.save()
            employment_formset.save()
            return render(request, 'jobs/success.html')


    else:
        applicant = Applicant(job=job)
        form = ApplicantForm(instance=applicant)
        bottom_form = ApplicantFormBottom(instance=applicant)
        education_formset = EducationInlineFormSet(instance=applicant)
        qualification_formset = QualificationInlineFormSet(instance=applicant)
        employment_formset = EmploymentInlineFormSet(instance=applicant)


    c = { 
    'job' : job,
    'form' : form ,
    'bottom_form' : bottom_form,
    'education_formset' : education_formset,
    'qualification_formset' : qualification_formset,
    'employment_formset' : employment_formset,
    }
    return render(request, 'jobs/application.html', c)

In order to customise the formset I defined the following:

class BaseFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(BaseFormSet, self).__init__(*args, **kwargs)
        for form in self.forms:
            form.empty_permitted = False

and pass use it as follows:

EducationInlineFormSet = inlineformset_factory(Applicant, Education, extra=1, can_delete=False, formset=BaseFormSet)

This returns the above error and having read around a lot I'm still none the wiser how I can keep passing an instance to the formset.

Any help would be greatly appreciated.

Regards,

Chris.

Community
  • 1
  • 1
Augier
  • 311
  • 2
  • 13

1 Answers1

5

I had a similar issue - the problem was the customised formset.

Try subclassing from BaseInlineFormSet (not BaseModelFormSet).

Here is the relevant section of the docs.

user3783363
  • 66
  • 1
  • 4