1 | | using djagno 5.0 |
2 | | |
3 | | 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. |
4 | | I am getting an error when the item_formset.is_valid() is called. see traceback below. no issues with the ModelForm or section_formset |
5 | | |
6 | | {{{ |
7 | | class SiteVisitItemForm(forms.ModelForm): |
8 | | |
9 | | class Meta: |
10 | | model = models.SiteVisitItem |
11 | | exclude = "report", "project", "action_for", "action_complete", "number" |
12 | | |
13 | | SiteVisitItemFormSet = forms.models.inlineformset_factory( |
14 | | models.SiteVisitReport, models.SiteVisitItem, |
15 | | form=SiteVisitItemForm, |
16 | | can_delete=True, |
17 | | extra=0, |
18 | | can_order=True, |
19 | | ) |
20 | | |
21 | | class SiteVisitSectionForm(forms.Form): |
22 | | section_name = forms.CharField(max_length=128, required=False, widget=forms.TextInput(attrs={"placeholder": "Section heading"})) |
23 | | section_text = forms.CharField(max_length=1000, required=False, widget=forms.Textarea(attrs={"placeholder": "Introduction", "style": "height: 100px;"})) |
24 | | |
25 | | SiteVisitSectionFormset = forms.formset_factory( |
26 | | form=SiteVisitSectionForm, can_delete=True, extra=0, can_order=True |
27 | | ) |
28 | | |
29 | | }}} |
30 | | |
31 | | |
32 | | {{{ |
33 | | |
34 | | Traceback (most recent call last): |
35 | | File "python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner |
36 | | response = get_response(request) |
37 | | File "python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response |
38 | | response = wrapped_callback(request, *callback_args, **callback_kwargs) |
39 | | File "python3.10/site-packages/django/views/generic/base.py", line 104, in view |
40 | | return self.dispatch(request, *args, **kwargs) |
41 | | File "python3.10/site-packages/fivestar_app/quality/views.py", line 129, in dispatch |
42 | | return super().dispatch(request, *args, **kwargs) |
43 | | File "python3.10/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch |
44 | | return super().dispatch(request, *args, **kwargs) |
45 | | File "python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch |
46 | | return handler(request, *args, **kwargs) |
47 | | File "python3.10/site-packages/fivestar_app/quality/views.py", line 222, in post |
48 | | if all([form.is_valid(), item_formset.is_valid(), section_formset.is_valid()]): |
49 | | File "python3.10/site-packages/django/forms/formsets.py", line 384, in is_valid |
50 | | self.errors |
51 | | File "python3.10/site-packages/django/forms/formsets.py", line 366, in errors |
52 | | self.full_clean() |
53 | | File python3.10/site-packages/django/forms/formsets.py", line 423, in full_clean |
54 | | for i, form in enumerate(self.forms): |
55 | | File "python3.10/site-packages/django/utils/functional.py", line 47, in __get__ |
56 | | res = instance.__dict__[self.name] = self.func(instance) |
57 | | File "python3.10/site-packages/django/forms/formsets.py", line 205, in forms |
58 | | return [ |
59 | | File "python3.10/site-packages/django/forms/formsets.py", line 206, in <listcomp> |
60 | | self._construct_form(i, **self.get_form_kwargs(i)) |
61 | | File "python3.10/site-packages/django/forms/models.py", line 1120, in _construct_form |
62 | | form = super()._construct_form(i, **kwargs) |
63 | | File "python3.10/site-packages/django/forms/models.py", line 737, in _construct_form |
64 | | kwargs["initial"] = self.initial_extra[i - self.initial_form_count()] |
65 | | |
66 | | Exception Type: KeyError at /quality/sitevisitreports/24022/2/items/ |
67 | | Exception Value: 0 |
68 | | }}} |
69 | | |
70 | | |
71 | | |
72 | | the traceback appears to refer to this part of model form: |
73 | | |
74 | | {{{ |
75 | | |
76 | | elif self.initial_extra: |
77 | | # Set initial values for extra forms |
78 | | try: |
79 | | kwargs["initial"] = self.initial_extra[i - self.initial_form_count()] |
80 | | except IndexError: |
81 | | pass |
82 | | }}} |
83 | | |
84 | | it appears that self.intial_extra may be a dictionary? |
85 | | |