Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » IndexError when validating model formsets [RSS Feed]

#1 Dec. 24, 2010 02:09:37

Andrew B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IndexError when validating model formsets


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:

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 = self.get_queryset()
File "/Library/Python/2.6/site-packages/django/db/models/query.py" in
__getitem__
172. return self._result_cache

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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 24, 2010 03:58:57

Andrew B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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
<and...@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:
>
> 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 = self.get_queryset()
> File "/Library/Python/2.6/site-packages/django/db/models/query.py" in
> __getitem__
>  172.             return self._result_cache
>
> 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » IndexError when validating model formsets [RSS Feed]

Board footer

Moderator control

Enjoy the 17th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support