直到今天,我的模型都是这样的:
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作为Decimal(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)
为Temperatura添加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。
非常感谢!
答案 0 :(得分:0)
问题是Django试图迁移旧版本的迁移文件而不是新版本。我不知道为什么。
我已经使用新的自定义解决了这个编辑该文件并删除.pyc文件强制重新加载它。