0
过多位直到今天,我得到了我的模特那样:Django模型十进制量化结果对当前上下文
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()
但我试图让温度和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和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:量化结果有太多的数字为当前上下文不管我做什么,它不工作。我没有在我的代码中使用任何DecimalField。
非常感谢!
也许你的数据库包含的数据不符合你的字段的限制吗?这听起来像... – Bakuriu
我已经截断了表格。 – Shaderlolz
你可以显示迁移文件吗? – Bakuriu