#35739: ModelFormset,  self.initial_extra issue
-------------------------------+--------------------------------------
     Reporter:  hcjohnston     |                    Owner:  (none)
         Type:  Bug            |                   Status:  closed
    Component:  Forms          |                  Version:  5.0
     Severity:  Normal         |               Resolution:  invalid
     Keywords:  model formset  |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  1              |                    UI/UX:  0
-------------------------------+--------------------------------------
Changes (by hcjohnston):

 * resolution:   => invalid
 * status:  new => closed


Old 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;"}))
>
> 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
>     kwargs["initial"] = 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
> }}}
>
> it appears that self.intial_extra may be a dictionary?

New description:



--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35739#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070191c58096dc-f964fff6-5fd5-4ba1-a25f-de19299084b8-000000%40eu-central-1.amazonses.com.

Reply via email to