Opened 2 weeks ago

Last modified 2 weeks ago

#35739 closed Bug

formset error — at Initial Version

Reported by: hcjohnston Owned by:
Component: Forms Version: 5.0
Severity: Normal Keywords: model formset
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

using djagno 5.0

I have an UpdateView which uses a ModelForm and two formsets ("item_formset" and "section_formset" below) to update a model. Both formsets have extra=0 and no initial data.
I am getting an error when the item_formset.is_valid() is called. see traceback below. no issues with the ModelForm or section_formset

class SiteVisitItemForm(forms.ModelForm):

    class Meta:
        model = models.SiteVisitItem
        exclude = "report", "project", "action_for", "action_complete", "number"

SiteVisitItemFormSet = forms.models.inlineformset_factory(
    models.SiteVisitReport, models.SiteVisitItem,
    form=SiteVisitItemForm,
    can_delete=True,
    extra=0,
    can_order=True,
)

class SiteVisitSectionForm(forms.Form):
    section_name = forms.CharField(max_length=128, required=False, widget=forms.TextInput(attrs={"placeholder": "Section heading"}))
    section_text = forms.CharField(max_length=1000, required=False, widget=forms.Textarea(attrs={"placeholder": "Introduction", "style": "height: 100px;"}))

    def clean(self):
        if self.cleaned_data["section_name"] == "" and self.cleaned_data["section_text"] == "" and self.cleaned_data["DELETE"] == False:
            self.add_error(None, "Please include either section name or introduction text")

SiteVisitSectionFormset = forms.formset_factory(
    form=SiteVisitSectionForm, can_delete=True, extra=0, can_order=True
)

Traceback (most recent call last):

File "python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner

response = get_response(request)

File "python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response

response = wrapped_callback(request, *callback_args, callback_kwargs)

File "python3.10/site-packages/django/views/generic/base.py", line 104, in view

return self.dispatch(request, *args, kwargs)

File "python3.10/site-packages/fivestar_app/quality/views.py", line 129, in dispatch

return super().dispatch(request, *args, kwargs)

File "python3.10/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch

return super().dispatch(request, *args, kwargs)

File "python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch

return handler(request, *args, kwargs)

File "python3.10/site-packages/fivestar_app/quality/views.py", line 222, in post

if all([form.is_valid(), item_formset.is_valid(), section_formset.is_valid()]):

File "python3.10/site-packages/django/forms/formsets.py", line 384, in is_valid

self.errors

File "python3.10/site-packages/django/forms/formsets.py", line 366, in errors

self.full_clean()

File python3.10/site-packages/django/forms/formsets.py", line 423, in full_clean

for i, form in enumerate(self.forms):

File "python3.10/site-packages/django/utils/functional.py", line 47, in get

res = instance.dict[self.name] = self.func(instance)

File "python3.10/site-packages/django/forms/formsets.py", line 205, in forms

return [

File "python3.10/site-packages/django/forms/formsets.py", line 206, in <listcomp>

self._construct_form(i, self.get_form_kwargs(i))

File "python3.10/site-packages/django/forms/models.py", line 1120, in _construct_form

form = super()._construct_form(i, kwargs)

File "python3.10/site-packages/django/forms/models.py", line 737, in _construct_form

kwargsinitial = self.initial_extra[i - self.initial_form_count()]

Exception Type: KeyError at /quality/sitevisitreports/24022/2/items/
Exception Value: 0

the traceback appears to refer to this part of model form:

        elif self.initial_extra:
            # Set initial values for extra forms
            try:
                kwargs["initial"] = self.initial_extra[i - self.initial_form_count()]
            except IndexError:
                pass

given that self.initial_extra = initial and initial is a dictionary, is IndexError the right exception here?

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top