Re: IndexError when validating model formsets
Nevermind.
I found that I was not instantiating the formset with the "instance"
argument, naming the parent object. Once that was added, everything
worked as designed.
-AB
On Thu, Dec 23, 2010 at 9:02 PM, Andrew Badstubner
<andrew@badstubner.com> wrote:
> I have a form that contains a parent object in the header and then
> lists the children as a formset in the same HTML <form>. I can create
> new "families" of parent objects and children just fine, but I can't
> edit them. I've tracked the error down to the line where the view is
> trying to instantiate the FormSet object for the children from the
> POST arguments. Python is complaining about an IndexError (see
> traceback below).
>
> I've found that I can modify parents just fine when there are zero
> children, but as soon as there are child objects, everything falls
> apart when the model formset is being instantiated.
>
> Has anyone seen this before?
>
> Many thanks for any help,
>
> Andy
>
>
> =================================
>
> Environment:
>
> Request Method: POST
> Request URL: http://127.0.0.1:8000/mixture/13/edit/
> Django Version: 1.2.1
> Python Version: 2.6.1
> Installed Applications:
> ['django.contrib.auth',
> 'django.contrib.contenttypes',
> 'django.contrib.sessions',
> 'django.contrib.sites',
> 'django.contrib.messages',
> 'django.contrib.admin',
> 'django.contrib.admindocs',
> 'form_utils',
> 'sgm.general',
> 'sgm.mixture',
> 'sgm.msds',
> 'sgm.account',
> 'sgm.errors']
> Installed Middleware:
> ('django.middleware.common.CommonMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'debug_toolbar.middleware.DebugToolbarMiddleware')
>
>
> Traceback:
> File "/Library/Python/2.6/site-packages/django/core/handlers/base.py"
> in get_response
> 100. response = callback(request,
> *callback_args, **callback_kwargs)
> File "/Library/Python/2.6/site-packages/django/contrib/auth/decorators.py"
> in _wrapped_view
> 25. return view_func(request, *args, **kwargs)
> File "/Users/abadstubner/Documents/Projects/specialty_gas_manager/../specialty_gas_manager/mixture/views.py"
> in edit_mixture
> 78. comp_formset = forms.CustomerMixtureComponentFormset(
> request.POST, prefix='comps' )
> File "/Library/Python/2.6/site-packages/django/forms/models.py" in __init__
> 698. queryset=qs)
> File "/Library/Python/2.6/site-packages/django/forms/models.py" in __init__
> 423. super(BaseModelFormSet, self).__init__(**defaults)
> File "/Library/Python/2.6/site-packages/django/forms/formsets.py" in __init__
> 47. self._construct_forms()
> File "/Library/Python/2.6/site-packages/django/forms/formsets.py" in
> _construct_forms
> 97. self.forms.append(self._construct_form(i))
> File "/Library/Python/2.6/site-packages/django/forms/models.py" in
> _construct_form
> 711. form = super(BaseInlineFormSet,
> self)._construct_form(i, **kwargs)
> File "/Library/Python/2.6/site-packages/django/forms/models.py" in
> _construct_form
> 447. kwargs['instance'] = self.get_queryset()[i]
> File "/Library/Python/2.6/site-packages/django/db/models/query.py" in
> __getitem__
> 172. return self._result_cache[k]
>
> Exception Type: IndexError at /mixture/13/edit/
> Exception Value: list index out of range
>
> Here's the POST data, in case anyone can see anything obvious there:
> account u'1'
> comments u''
> comps-0-concentration u'20%'
> comps-0-customermixture u'13'
> comps-0-gas u'Benzene'
> comps-0-id u'16'
> comps-0-solubility u''
> comps-1-concentration u'20%'
> comps-1-customermixture u'13'
> comps-1-gas u'Acetaldehyde'
> comps-1-id u'17'
> comps-1-solubility u''
> comps-2-concentration u'Balance'
> comps-2-customermixture u'13'
> comps-2-gas u'Carbon Dioxide'
> comps-2-id u'18'
> comps-2-solubility u''
> comps-INITIAL_FORMS u'3'
> comps-MAX_NUM_FORMS u''
> comps-TOTAL_FORMS u'3'
> csrfmiddlewaretoken u'3...a' #redacted
> engineering_controls u'1'
> hazard_warning u'2'
> name u'Sample Mixture3'
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home