简体   繁体   English

升级 Django 和 Python 版本,将 FloatField 的默认值设置为字符串的问题

[英]upgrading Django and Python versions, problem with default value for FloatField set to string

In a model using Django 1.8 and Python 2.7 we had a migration that accidentally set the default value for a FloatField to an empty string (default=''), which Django appears to have interpreted as a bytes-string: In a model using Django 1.8 and Python 2.7 we had a migration that accidentally set the default value for a FloatField to an empty string (default=''), which Django appears to have interpreted as a bytes-string:

class Migration(migrations.Migration):

    dependencies = [
        ('reo', '0030_merge'),
    ]

    operations = [
        migrations.CreateModel(
            name='ProfileModel',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('pre_setup_scenario_seconds', models.FloatField(default=b'', null=True)),
            ],
        ),
    ]

After upgading to Django 2.2 and Python 3.6, Django gives the warning with manage.py migrate that there are un-migrated changes.升级到 Django 2.2 和 Python 3.6 后,Django 使用manage.py migrate发出警告,提示存在未迁移的更改。 After changing the default value from '' to None and running manage.py makemigrations we get:将默认值从''更改为None并运行manage.py makemigrations我们得到:

operations = [
        migrations.AlterField(
            model_name='profilemodel',
            name='pre_setup_scenario_seconds',
            field=models.FloatField(default=None, null=True),
        ),
]

Then running manage.py migrate yields:然后运行manage.py migrate产生:

nlaws-> python manage.py migrate
Operations to perform:
  Apply all migrations: auth, contenttypes, django_celery_results, proforma, reo, resilience_stats, sessions, summary, tastypie
Running migrations:
  Applying reo.0050_auto_20191030_1738...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 124, in _alter_field
    new_db_params, strict,
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 648, in _alter_field
    old_default = self.effective_default(old_field)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 784, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Users/nlaws/.virtualenv/api/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1739, in get_prep_value
    return float(value)
ValueError: could not convert string to float: 

Does anyone know how to fix this?有谁知道如何解决这一问题?

It appears to be related to https://code.djangoproject.com/ticket/28431 , which had a string value set as default for a binary field - and appears to have been a bug in the Django source code.它似乎与https://code.djangoproject.com/ticket/28431相关,该字符串值设置为二进制字段的默认值 - 并且似乎是 Django 源代码中的错误。

The suggestion from return float(value) ValueError: could not convert string to float in Django models was helpful in fixing this problem, specifically we ended up: return float(value) ValueError: could not convert string to float in Django 模型的建议有助于解决这个问题,特别是我们结束了:

  1. Creating a new float field2创建一个新的浮点字段2
  2. Run a management command to get the data from one field, convert it to a float, and save it to the other field运行管理命令从一个字段获取数据,将其转换为浮点数,并将其保存到另一个字段
  3. Delete the first (string) field删除第一个(字符串)字段
  4. Rename the second field (the float field) to whatever name you need it to have.将第二个字段(浮动字段)重命名为您需要的任何名称。

Step 2 was accomplished with a migrations.RunSQL with第 2 步是使用migrations.RunSQL完成的

UPDATE reo_profilemodel SET pre_setup_scenario_seconds2 = cast(pre_setup_scenario_seconds as double precision)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM