Opened 8 years ago

Closed 8 years ago

#6211 closed (invalid)

newforms.models.save_instance boolean field TypeError

Reported by: sethtrain Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When a boolean field (checkbox) isn't sent (because it isn't checked) i get a failure "Type Error: argument of type 'bool' is not iterable"

Attachments (2)

save_instance.2.diff (1.1 KB) - added by sethtrain 8 years ago.
Patch to fix TypeError? issue. Checks for fields before acting upon it.
save_instance.diff (1.0 KB) - added by sethtrain 8 years ago.
fix patch index

Download all attachments as: .zip

Change History (8)

comment:1 Changed 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

This bug report is lacking a lot of information. How do we reproduce this problem? What are the circumstances under which the problem occurs?

comment:2 Changed 8 years ago by sethtrain

Sorry for the lack of information. Here is a bit more detailed information, I was busy at the time of creating this ticket. The problem seemed to be with code lines 42 and 43, because all worked when I commented them out.

if fields and f.name not in fields:

continue

After uncommenting those line and testing again, all seems well. This is weird. Sorry for the waste of time.

comment:3 Changed 8 years ago by mtredinnick

  • Resolution set to invalid
  • Status changed from new to closed

Closing, based on previous comment.

comment:4 Changed 8 years ago by sethtrain

  • Resolution invalid deleted
  • Status changed from closed to reopened

After deploying site I am getting this error again. I am going to do my best to document my issues. Here is the traceback:

Environment:

Request Method: POST
Request URL: http://market_stand.cabedgedev.com/admin/products/categories/new/
Django Version: 0.97-pre-SVN-6941
Python Version: 2.5.1
Installed Applications:
['django.contrib.admin',

'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'market_stand.products',
'market_stand.orders',
'market_stand.shipping',
'market_stand.customers',
'market_stand.shop']

Installed Middleware:
('django.middleware.common.CommonMiddleware',

'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware')

Traceback:
File "/apps/shared/django/6941/django/core/handlers/base.py" in get_response

  1. response = callback(request, *callback_args, callback_kwargs)

File "/apps/market_stand/current/market_stand/products/categories/views.py" in new

  1. category = form.save()

File "/apps/market_stand/current/market_stand/products/categories/forms.py" in save

  1. return forms.models.save_instance(self, instance, commit)

File "/apps/shared/django/6941/django/newforms/models.py" in save_instance

  1. if fields and f.name not in fields:

Exception Type: TypeError at /admin/products/categories/new/
Exception Value: argument of type 'bool' is not iterable

Here is the form I am using to save the instance:

class CategoryForm(forms.Form):

"""(CategoryForm description)"""
name = forms.CharField(max_length=100)
parent = forms.ModelChoiceField(queryset=Category.objects.all(), required=False)


def clean_parent(self):

try:

category = Category.objects.get(nameexact=self.cleaned_dataparent?)

except Category.DoesNotExist:

return self.cleaned_dataparent?

if category.has_parent():

raise forms.ValidationError('Categories are only meant to be nested two deep. Your parent category can\'t have children.')

return self.cleaned_dataparent?


def save(self, id=None, commit=True):

if not id:

instance = Category()

else:

instance = Category.objects.get(pk=id)

return forms.models.save_instance(self, instance, commit)

Changed 8 years ago by sethtrain

Patch to fix TypeError? issue. Checks for fields before acting upon it.

Changed 8 years ago by sethtrain

fix patch index

comment:5 Changed 8 years ago by sethtrain

This also doesn't have anything to do with a checkbox like I said in the initial comment. This has everything to do with not sending anything via the fields kwarg to the method.

comment:6 Changed 8 years ago by Karen Tracey <kmtracey@…>

  • Resolution set to invalid
  • Status changed from reopened to closed

In your code:

return forms.models.save_instance(self, instance, commit)

should be:

return forms.models.save_instance(self, instance, commit=commit)

You can't pass commit in as simply a positional parameter without also passing in fields and fail_message ahead of it. If you want to pass just commit, you have to pass it as a keyword argument. With the call you are making, whatever value commit has is what save_instance is using for fields, which is why you see the error about a bool not being iterable.

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