我正在做最简单的操作,并得到一个错误...我拉模型实例,更新一些字段,然后保存我修改的那些字段。我收到一个错误,指出这些字段中的一个不允许空值。该布尔型字段在models.py中具有默认值,在数据库中为AND,它是我为其保存值的字段之一。Django脚本无法保存模型实例,抱怨值为null的字段...?
models.py
class Person(TimeStampedModel):
# ...
precertified = models.BooleanField(default=False)
logic.py
try:
print('Getting Person %s...' % record['nurturing_id'])
person = Person.objects.get(pk=record['nurturing_id'])
except Person.DoesNotExist:
person = None
print('Person %s no longer exists.' % record['nurturing_id'])
if person:
person.uncorrected_address = person.address
person.uncorrected_address_line_two = person.address_line_two
person.uncorrected_city = person.city
person.uncorrected_state = person.state
person.uncorrected_zipcode = person.zipcode
person.address = record['Address']
person.address_line_two = record['Address Line Two']
person.city = record['City']
person.state = record['State']
person.zipcode = record['Zipcode']
person.precertified = True
person.precertified_check = now
person.save(update_fields=['precertified', 'address', 'address_line_two', 'city', 'state', 'zipcode', 'uncorrected_address', 'uncorrected_address_line_two', 'uncorrected_city', 'uncorrected_state', 'uncorrected_zipcode', ])
使用person.save()
也会失败。
回溯:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/sites/easy/apps/JSM/logic.py", line 1349, in precertify_contacts
person.save(update_fields=['precertified', 'address', 'address_line_two', 'city', 'state', 'zipcode', 'uncorrected_address', 'uncorrected_address_line_two', 'uncorrected_city', 'uncorrected_state', 'uncorrected_zipcode', ])
File "/Environments/easy/local/lib/python2.7/site-packages/model_utils/tracker.py", line 134, in save
ret = original_save(**kwargs)
File "/Environments/easy/src/easyapps/nurturing/models.py", line 248, in save
super(Person, self).save(*args, **kwargs)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/base.py", line 801, in _save_table
forced_update)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/base.py", line 851, in _do_update
return filtered._update(values) > 0
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/query.py", line 645, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1149, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Environments/easy/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
IntegrityError: null value in column "precertified" violates not-null constraint
DETAIL: Failing row contains (165771, 2016-11-16 01:48:15.613901+00, 2017-02-10 14:09:34.11354+00, General Manager, Prospect, null, , [email protected], 1671 SOME RD, None, LINTHICUM, MD, 21090-9999, null, null, null, null, null, null, 999999, null, null, null, null, , Company Name, null, null, null, null, null, null, , , null, null, null, f, null, null, null, 005o0000001fAnfAAE, , Market Specific Mass Email, null, null, 00Qo0000006RxoUEAS, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, f, null, null, 2017-02-10 14:09:34.061993+00, 1671 Some Rd, null, Linthicum, MD, 21090).
我必须失去了一些东西愚蠢。有人给我提出这个提示吗?
看起来真的很奇怪,您是否尝试在person.save()之前打印(person.precertified)?还要检查models.py中是否存在此类的save()方法;看看信号 - 例如pre-save – Satevg
@Satevg:是的!我不能相信我错过了这一点。我有一个基于模型以前版本的自定义保存方法,如果地址发生更改,则将优先级设置为无。 *拍额头*谢谢! – Psyferre
添加评论为答复:) – Satevg