Django脚本无法保存模型实例,抱怨一个值为null的字段...?

时间:2017-02-10 14:57:53

标签: django django-models

我正在执行最简单的操作并收到错误...我正在拉模型实例,更新某些字段,然后保存我修改过的字段。我收到一个错误,说明其中一个字段不允许使用空值。该boolean字段在models.py中有默认值,在数据库中有AND,它是我为其保存值的字段之一。

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@email.com, 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).

我必须失去一些愚蠢的东西。有人用cluebat来打扰我吗?

2 个答案:

答案 0 :(得分:0)

就像@Satevg建议的那样,我有一个基于以前版本的模型的自定义保存方法。如果任何地址字段被更改,它会将预先设置为None(在以前的版本中它不是布尔值)。

(@ Satevg - 如果你想重新发表你的评论作为答案我会接受它,所以你得到一些积分。)

答案 1 :(得分:0)

看起来很奇怪,你在print(person.precertified)之前尝试person.save()吗?

另请检查save()中是否有此类的models.py方法;看看信号 - 例如pre-save