Django Models Decimal quantize 结果对于当前上下文有太多数字

Posted

技术标签:

【中文标题】Django Models Decimal quantize 结果对于当前上下文有太多数字【英文标题】:Django Models Decimal quantize result has too many digits for current context 【发布时间】:2017-07-10 05:29:30 【问题描述】:

直到今天我的模型都是这样的:

class Ambiente1m(models.Model):
    Zona = models.CharField(max_length=10,default=0)
    Timestamp = models.DateTimeField(default=datetime.datetime.now)
    Temperatura1 = models.IntegerField()
    Humedad1 = models.IntegerField()

但我尝试将 Temperatura 和 Humedad 作为十进制 (4,2) 并添加 2 列:

class Ambiente1m(models.Model):
    Zona = models.CharField(max_length=10,default=0)
    Timestamp = models.DateTimeField(default=datetime.datetime.now)
    Temperatura1 = models.DecimalField(max_digits=4, decimal_places=2)
    Humedad1 = models.DecimalField(max_digits=4, decimal_places=2)
    Temperatura2 = models.DecimalField(max_digits=4, decimal_places=2)
    Humedad2 = models.DecimalField(max_digits=4, decimal_places=2)

添加 Humedad -1 的默认值和 Temperatura 的 99.99。

然后我使用了 python manage.py makemigrations,并进行了迁移。 我得到以下跟踪:

python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, sessions, auth, SmartFICApp
Running migrations:
  Applying SmartFICApp.0017_auto_20170220_1830...Traceback (most recent call las           t):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.p           y", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.p           y", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",            line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",            line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/m           igrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py"           , line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini           tial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py"           , line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_           initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py"           , line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py           ", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec           t_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/f           ields.py", line 62, in database_forwards
    field,
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py           ", line 382, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py           ", line 145, in column_sql
    default_value = self.effective_default(field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py           ", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.           py", line 1612, in get_db_prep_save
    self.max_digits, self.decimal_places)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/operation           s.py", line 493, in adapt_decimalfield_value
    return utils.format_number(value, max_digits, decimal_places)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", lin           e 200, in format_number
    value = value.quantize(decimal.Decimal(".1") ** decimal_places, context=cont           ext)
  File "/usr/lib/python2.7/decimal.py", line 2469, in quantize
    'quantize result has too many digits for current context')
  File "/usr/lib/python2.7/decimal.py", line 3873, in _raise_error
    raise error(explanation)
decimal.InvalidOperation: quantize result has too many digits for current context

问题是,我尝试获取 models.IntegerField() 版本:

class Ambiente1m(models.Model):
    Zona = models.CharField(max_length=10,default=0)
    Timestamp = models.DateTimeField(default=datetime.datetime.now)
    Temperatura1 = models.IntegerField()
    Humedad1 = models.IntegerField()
    Temperatura2 = models.IntegerField()
    Humedad2 = models.IntegerField()

我正在使用 PostgreSQL。

但我得到了同样的错误,decimal.InvalidOperation: quantize result has too many numbers for current context 不管我做什么,它都不起作用。我的代码中没有使用任何 DecimalField。

非常感谢!

【问题讨论】:

也许您的数据库包含不符合您的字段约束的数据?听起来像…… 我已经截断了表格。 你能显示迁移文件吗? 【参考方案1】:

问题是 Django 试图迁移旧版本的迁移文件而不是新版本。我不知道为什么。

我已经解决了通过新的自定义编辑该文件并删除强制重新加载它的 .pyc 文件的问题。

【讨论】:

以上是关于Django Models Decimal quantize 结果对于当前上下文有太多数字的主要内容,如果未能解决你的问题,请参考以下文章

django实现读写分离

Django之ORM练习

Django ORM训练专题

Django Manage File

Django之Orm操作数据

django_filters结合drf