gpt4 book ai didi

python - Django 'bool' 对象在模型保存时不可迭代

转载 作者:行者123 更新时间:2023-11-28 21:49:59 24 4
gpt4 key购买 nike

我正在尝试更新我的模型,因此当通过管理员检查 BooleanField 时,它会更新所有其他行以重置为 False (0)。但是当我继续这样做时,它只会返回以下 TypeError: 'bool' object is not iterable

这是我的模型:

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)

我正在尝试使用名为 make_primary() 的自定义方法在 save() 期间更新我的行。感觉就像我需要做的事情非常简单明了。我是 Django 的新手,所以它有点学习曲线。

如有任何帮助和建议,我们将不胜感激。

谢谢:)

编辑:

根据这里的要求,下面是我的回溯/错误。此外,我更新了我的代码以使用 exclude 代替,并且错误已更改为:'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

最佳答案

此行无效:

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

当您执行 id!=self.id 时,表达式的计算结果为 False,因此它与执行 filter(False) 相同.这是您的回溯消息中“不可迭代”的 bool 对象,它实际上与您的 BooleanField 没有任何关系。

当您执行 filter(id=self.id) 时,您将关键字参数传递给 filter 方法。 Django 没有办法将不等于作为过滤器中的关键字参数。您可以使用 exclude()相反。

MyModel.objects.exclude(id=self.id)

关于python - Django 'bool' 对象在模型保存时不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com