gpt4 book ai didi

Django:DecimalField 接受 float ,但这应该会引发错误

转载 作者:行者123 更新时间:2023-12-04 18:11:22 30 4
gpt4 key购买 nike

我有一个带有小数字段的 Django 模型:

class User(models.Model):
...
balance = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
...

我的应用程序在我的开发机器(Python 2.7.1、Mac OSX、Django 1.4、MySQL InnoDB)和生产服务器(Python 2.6.5、Ubuntu、Django 1.4、MySQL ISAM)上运行

问题出在我的开发机器上。此代码可以正常工作而不会引发错误:
>>> u = User.objects.get(pk=1)
>>> u.balance
Decimal('9.00')
>>> u.balance=1.44
>>> u.save()
>>> u.balance
1.44

据我了解, u.save() 应该通过以下错误,它在我的生产实例上发生:
>>> u.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 463, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 529, in save_base
rows = manager.using(using).filter(pk=pk_val)._update(values)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 557, in _update
return query.get_compiler(self.db).execute_sql(None)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 986, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 808, in execute_sql
sql, params = self.as_sql()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 951, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py", line 873, in get_db_prep_save
return connection.ops.value_to_db_decimal(self.to_python(value),
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py", line 847, in to_python
return decimal.Decimal(value)
File "/usr/lib/python2.6/decimal.py", line 649, in __new__
"First convert the float to a string")
TypeError: Cannot convert float to Decimal. First convert the float to a string

那么为什么我的开发实例没有抛出这个错误呢?出现问题是因为我 checkin 了一些通过单元测试的错误代码。这个错误应该被抛出。我放了我的环境细节,因为它必须是由于这些差异之一。我怀疑 MySQL ISAM 与 InnoDB,但完全不确定。

谢谢!

最佳答案

运行 Python 2.7 的开发环境不应该抛出该错误,但运行 Python 2.6 的生产环境应该。

来自 Decimal objects 上的 Python 文档:

Changed in version 2.7: The argument to the constructor is now permitted to be a float instance.

关于Django:DecimalField 接受 float ,但这应该会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12596834/

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