How to set at least one inline form required in django inline formset

I was working on my client project. The project was basically a teaching management system. I implemented the Django inline formset in his project. 

Scenario.

We have a lesson request form. Students can request lessons by sending lesson requests to the teacher. I am attaching a screenshot to make it more clear.

The client wanted at least one lesson inline form mandatory. I just used this setting in his project.

LessonFormSet = inlineformset_factory(
    LessonRequest, Lesson, form=StudentLessonForm,
    extra=1, can_delete=True, can_delete_extra=True,
)

And when I was submitting the form without filling in the lesson information, I was getting the message "This field is required" under the Date and Time fields. It means that this inline form is mandatory right? but a big no. why?

Because my client did was he filled out the all inline lesson form fields and also selected the delete checkbox. Then he observed the form have been submitted without any lesson. Because this is an inline form specified by extra=1 and can_delete_extra=True have deleted this inline form by selecting the delete checkbox.

Solution.

Let's move toward a solution, How can you solve this if you are facing the same thing?

Django inlineformset_factory function these 2 parameters min_num and validate_min will help to solve this problem.

min_num: gives you the ability to limit the minimum number of forms the formset will display.

validate_min: If ``validate_min=True`` is passed then validation will also check that the number of forms in the data set, minus those marked for deletion is greater than or equal to min_num.

Then I specified this setting.

LessonFormSet = inlineformset_factory(
    LessonRequest, Lesson, form=StudentLessonForm,
    extra=1, can_delete=True, can_delete_extra=True,
    min_num=1, validate_min=True,
)

And it solved my problem but now I have 2 extra inline forms instead of 1. because Django inline empty forms are determined by extra + min_num (means 1+1=2 extra inline forms)

So that's why I specified extra=0 and min_num=1

# SOULTION

LessonFormSet = inlineformset_factory(
    LessonRequest, Lesson, form=StudentLessonForm,
    extra=0, can_delete=True, can_delete_extra=True,
    min_num=1, validate_min=True,
)

Now if my client selects the delete checkbox following the above scenario. Then validate_min=True validation checked that the number of forms in the data set, minus those marked for deletion, is greater than or equal to min_num.

the number of forms in the data set - those marked for deletion > min_num

1 - 1 > 1 (validation failed here). So that formset will raise a validation that at least 1 form is required..

I hope it clears for you. If you find it useful then show your support and love in the comment section or by buying me a coffee :)

Related Article.

Django Inline Formset Factory with Examples.

I would greatly appreciate it if you subscribe to my YouTube channel, Let's Code More

 ⋅  2 comments have been posted.
    May 25, 2023, 12:37 p.m. - Alan Keys  
    Hi Seher, i followed up your previous post on Django Inline Formset Factory with Examples, I am new to django logics and my question is, what if i want some of the inline fields to appear only based on above fields? for example am collecting information of households, and if one is married then it should show the spouse inlineform and if single just skip to submit. Thank you in advance
    Reply
    May 6, 2023, 2:54 p.m. - KERIREME  
    Hello, please How to use Django Inline Formset Factory to create products using a Variants csv file? Thank you so much.
    Reply

Your comment

Required for comment verification
claps