带有GenericIPAddressField的Django迁移错误

时间:2019-04-09 12:21:13

标签: django django-models

嘿,我现在正在研究某个项目,我想在每个模型中添加IP地址字段

  

ip_address = models.GenericIPAddressField()

像这样添加 现在,当我迁移它时,我得到了一个错误 像这样

  

if值和':'值:   TypeError:类型为'int'的参数不可迭代

0005_remove_documentvault_ip_address

class Migration(migrations.Migration):

dependencies = [
    ('master', '0005_remove_documentvault_ip_address'),
]

operations = [
    migrations.AddField(
        model_name='documentvault',
        name='ip_address',
        field=models.GenericIPAddressField(default=1),
        preserve_default=False,
    ),
]

错误消息:

    Operations to perform:
  Apply all migrations: admin, auth, contenttypes, master, sessions, users
Running migrations:
  Applying master.0006_documentvault_ip_address...Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/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 "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/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 "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
    field,
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/mysql/schema.py", line 42, in add_field
    super().add_field(model, field)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 421, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 168, in column_sql
    default_value = self.effective_default(field)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 239, in effective_default
    return field.get_db_prep_save(default, self.connection)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 790, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1936, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1943, in get_prep_value
    if value and ':' in value:
TypeError: argument of type 'int' is not iterable

1 个答案:

答案 0 :(得分:2)

迁移(也许还有您的模型)具有default=1。这不是有效的IP地址。

您可以用有效的IP地址替换模型中的默认值,也可以删除默认值并设置null=True

然后删除0006迁移(为安全起见先备份它,或确保它在版本控制中)。最后,重新运行makemigrations并进行迁移。