Modelformset_factory including an empty list object as part of the management_form data displayed on screen
When rendering a formset created using modelformset_factory I am experiencing differing results between the local running instance of my application compared to the version running on my server.
On both versions the application the files below are included the following:
forms.py
class NewCourseHoleForm(ModelForm):
class Meta:
model = CourseHole
fields = [
'hole_number',
'hole_name',
'par',
'stroke_index',
'length',
]
def __init__(self, *args, **kwargs):
super(NewCourseHoleForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_tag = False
self.helper.form_show_errors = True
self.helper.form_show_labels = False
self.helper.form_class = 'form-inline'
self.helper.use_custom_control = True
self.helper.layout = Layout(
Div(
Div(
Div(Field('hole_name', css_class='text-center'), css_class="col-3"),
Div(
Field('hole_number', css_class='d-none'),
css_class="d-none"),
Div(Field('length', min=50, max=800), css_class="col-3"),
Div(Field('par', min=3, max=5), css_class="col-3"),
Div(Field('stroke_index', min=1, max=18), css_class="col-3"),
css_class='row',
),
css_class='col-12'
),
)
hole_formset = modelformset_factory(
CourseHole,
form=NewCourseHoleForm,
min_num=18,
max_num=18,
# validate_max=True
# extra=18,
# can_delete=False,
)
views.py
class CourseDetailView(LoginRequiredMixin, DetailView):
login_url = '/login/'
redirect_field_name = 'redirect_to'
model = Course
slug_field = 'uuid'
slug_url_kwarg = 'uuid'
template_name = 'courses/course_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
course = Course.objects.filter(
uuid=self.kwargs.get('uuid')).first()
context['title'] = f'{course.name} - Detail'
teebox_check = CourseTeeBox.objects.filter(
course=course).order_by('-course_length')
context['teeboxes'] = teebox_check
context['teeboxForm'] = f.NewCourseTeeBoxForm(
prefix='teebox', course=course)
holes = [{'hole_number': n, 'hole_name': f'Hole {n}'} for n in range(1,19)]
context['holes_formset'] = f.hole_formset(
queryset=CourseHole.objects.none(), prefix='holes', initial=holes)
return context
def get_queryset(self):
queryset = super(CourseDetailView, self).get_queryset()
return queryset
template.html
<div class="row">
{{ holes_formset.management_form }}
{% for form in holes_formset %}
{% crispy form %}
{{form.id}}
{% endfor %}
</div>
When rendering the page within my local testing the result looks like the below image: with the html looking as expected:
When running the exact same code on a server it results in the following with a empty list object added prior to the forms and the hidden formset management_form:
I have tried multiple versions of the formset and removed the initial data as well as updating the attached queryset to include existing items and still the formset returns the empty []
prior to the management_form. Any advice would be greatly appreciated.