Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 'bool' object is not iterable on model save

Tags:

python

django

I'm trying to update my model, so when a BooleanField is checked via the admin, it updates all the other rows to be reset to False (0). But when I keep doing this it just returns the following TypeError: 'bool' object is not iterable

Here is my model:

class Contact(models.Model):
    name            = models.CharField(max_length=255)
    telephone       = models.CharField(max_length=255,blank=True)
    email           = models.CharField(max_length=255,blank=True)
    primary_contact = models.BooleanField('Primary Contact')

    def __unicode__(self):
        return self.name

    def make_primary(self):
        Contact.objects.filter(id!=self.id).update(primary_contact=False)

    def save(self, *args, **kwargs):
        if (self.primary_contact == True):
            self.make_primary()
        super(Contact, self).save(*args, **kwargs)

I'm Trying to update my rows during save() using a custom method called make_primary(). It feels like there is something super simple and obvious that I need to do. I'm new to Django, so it's a bit of a learning curve.

Any help and advice would be greatly appreciated.

Thanks :)

EDIT:

As requested here is my traceback / error below. Also, I updated my code to use exclude instead and the error has changed to: 'long' object is not iterable

Environment:


Request Method: POST
Request URL: http://localhost:8000/admin/contact/contact/1/

Django Version: 1.8.2
Python Version: 2.7.6
Installed Applications:
('grappelli',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'tinymce',
 'adminsortable',
 'taggit',
 'contact')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/adminsortable/admin.py" in change_view
  231.             form_url='', extra_context=extra_context)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in change_view
  1519.         return self.changeform_view(request, object_id, form_url, extra_context)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in inner
  145.                     return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1467.                 self.save_model(request, new_object, form, not add)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_model
  1078.         obj.save()
File "/Users/[hidden]/Sites/[hidden]/contact/models.py" in save
  37.           self.make_primary()
File "/Users/[hidden]/Sites/[hidden]/contact/models.py" in make_primary
  28.       for oc in other_contacts:

Exception Type: TypeError at /admin/contact/contact/1/
Exception Value: 'long' object is not iterable
like image 543
Liam Chapman Avatar asked Nov 15 '25 12:11

Liam Chapman


1 Answers

This line is invalid:

MyModel.objects.filter(id!=self.id)

When you do id!=self.id, the expression evaluates to False, so it's the same as doing filter(False). That's the bool object that is 'not iterable' in your traceback message, it doesn't actually have anything to do with your BooleanField.

When you do filter(id=self.id), you are passing a keyword argument to the filter method. Django doesn't have a way to do not equal as a keyword argument in a filter. You can use exclude() instead.

MyModel.objects.exclude(id=self.id)
like image 133
Alasdair Avatar answered Nov 17 '25 09:11

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!