【问题标题】:Django model instance full_clean method, is this right?django模型实例full_clean方法,对吗?
【发布时间】:2016-05-17 19:33:25
【问题描述】:

我想做的是编写允许我从 csv 文件批量加载 Django 对象实例的代码。显然我应该在保存任何东西之前先检查所有数据。

tl;dr:full_clean() 方法不会捕捉到即将尝试在没有null=True 的字段中保存 None 的尝试。看起来很反常。这是设计使然,如果是,为什么? Django 的 bug 比我用过的任何其他软件都要少,所以“Bug!”似乎最不可能。

完整版。我认为可行的是,为每一行创建一个对象实例,用电子表格中的数据填充字段,然后调用 full_clean 方法。 IE。 (大纲)

from django.core.exceptions import ValidationError
...

# upload a CSV file and open with a csvreader
errors=[]
for rownumber, row in enumerate(csvreader):

    o = SomeDjangoModel()
    o.somefield = row[0]  # repeated for all input data row[1] ...

    try:
        reason = ""
        o.full_clean()
    except ValidationError as e:
        reason = "Row:{} Reason:{}".format( rownumber, str(e))
        errors.append( reason)
        # reason, together with the row-number of the csv file, fully explains
        # what is wrong.

# end of loop
if errors:
    # display errors to the user for him to fix
else:
    # repeat the loop,  doing .save() instead of .full_clean() 
    # and get database integrity errors trying to save Null in non-null model field.

问题是,.full_clean() 在没有 null=True 的字段中无法捕获 None 值

我该怎么办?想法包括

  1. 将整个事务包装在一个事务中,在异常处理程序中执行一批 o.save(),然后回滚整个事务,除非没有错误。但是,当可能 90% 的尝试都会以微不足道的方式出错时,为什么还要打扰数据库呢?

  2. 通过表单输入数据,即使没有与用户进行表单级别的每行交互。

  3. 在不应该的地方手动测试无。但是 .full_clean 还没有检查什么?

我可以理解,最终捕获数据库完整性错误的唯一方法是尝试存储数据,但为什么 Django 不能单独在 null=False 字段中捕获 None 呢?

顺便说一句,这是 Django 1.9.6

添加了细节。这是模型定义的相关字段

class OrderHistory( models.Model):
    invoice_no = models.CharField( max_length=10, unique=True)         # no default
    invoice_val= models.DecimalField( max_digits=8, decimal_places=2)  # no default
    date       = models.DateField( )                                   # no default

这就是正在发生的事情,从 python manage.py shell 完成,以证明 .full_clean 方法无法发现 n

>>> from orderhistory.models import OrderHistory
>>> from datetime import date
>>> o = OrderHistory( date=date(2010,3,17), invoice_no="21003163")
>>> o.invoice_val=None 
>>> o.full_clean()  # passes clean
>>> o.save() # attempt to save this one which has passed full_clean() validation
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/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 "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "invoice_val" violates not-null constraint
DETAIL:  Failing row contains (2, 21003163, , , 2010-03-17, , null, null, null, null, null, null).
>>>
>>> p = OrderHistory( invoice_no="21003164") # no date
>>> p.date=None
>>> p.full_clean()                           # this DOES error as it should
  Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/nigel/.virtualenvs/edge22/lib/python3.4/site-packages/django/db/models/base.py", line 1144, in full_clean
  raise ValidationError(errors)
django.core.exceptions.ValidationError: {'date': ['This field cannot be null.']}
>>>

【问题讨论】:

  • 您的 csv 文件中如何表示 None 值?您的代码缩进有错误,请参阅最后 4 行检查错误并保存的位置。
  • 这不是代码错误。我在循环过程中积累了错误。如果有错误,则在循环之后显示所有错误,否则我重复循环执行 save() 而不是 full_clean()。 (这可能会引发数据库完整性错误,但这与这个问题无关)。
  • CSV 文件中的 Null 是两个逗号,它们之间没有任何内容。我还将数字列中的全空白字符串视为 null(这是用户将数据间隔而不是删除,这是一种常见的电子表格反模式)。

标签: python django


【解决方案1】:

我刚刚在 shell 中重复了您的步骤,并且 full_clean() 为 None 值触发 ValidationError:

>>> from orders.models import OrderHistory
>>> o = OrderHistory()
>>> o.full_clean()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/oz/.virtualenvs/full_clean_test/lib/python2.7/site-packages/django/db/models/base.py", line 1144, in full_clean
    raise ValidationError(errors)
ValidationError: {'date': [u'This field cannot be null.'], 'invoice_val': [u'This field cannot be null.'], 'invoice_no': [u'This field cannot be blank.']}

我已经在 OSX 上使用 Django 1.9.6 和 Python 2.7.10 以及 Ubuntu 上使用 Python 3.4.3 对新项目进行了测试。

尝试从您的项目中删除所有 *.pyc 文件。如果这不起作用,请删除您的虚拟环境,创建新环境并重新安装您的依赖项。

【讨论】:

  • 对不起,但这不是问题的答案。我想知道的是,为什么 full_clean 方法无法检测到这些空值。当然,我可以添加很多特殊情况的代码来自己检查数据,但是鉴于它对模型的了解,Django 不应该为我做这件事吗?
  • 就像你自己说的,这不太可能是 Django 或 full_clean() 方法错误。我的猜测是您的 csv 阅读器的行为不像您预期​​的那样,并且您的空值(两个逗号之间没有任何内容)不会被读取为 python None 类型,而可能是空白字符串。您可以添加一些调试日志/打印并检查您为空值获得的值和类型 print "value: {}, type: {}".format(o.somefield, type(o.somefield)) 吗?
  • 添加到问题的详细信息。未显示的字段具有默认值或 null=True。是 o.invoice_val=None 通过 full_clean()
  • 我已经更新了我的答案,我在 cmets 中没有足够的空间。 :)
  • 谢谢。很高兴知道这是我的代码中的某些内容,而不是 Django 中的错误功能!我现在要到下周二才上班,但我会在弄清楚我为什么会出现不良行为后进行更新。
猜你喜欢
  • 2022-01-24
  • 2012-11-02
  • 2023-03-28
  • 2011-01-05
  • 1970-01-01
  • 2023-02-08
  • 2015-09-20
  • 2011-01-21
  • 2020-06-19
相关资源
最近更新 更多