Ошибка миграции Django с GenericIPAddressField

#django #django-models

#django #django-модели

Вопрос:

эй, я работаю над каким-то проектом, теперь я хочу добавить поля IP-адреса в каждую модель

ip_address = модели.GenericIPAddressField()

добавляя вот так, теперь, когда я переношу его, я получаю ошибку, подобную этой

если значение и ‘:’ в значении: 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:

Миграция (и, возможно, ваша модель тоже) имеет default=1 . Это недопустимый IP-адрес.

Вы можете либо заменить значение по умолчанию в вашей модели на действительный IP-адрес, либо удалить значение по умолчанию и установить null=True .

Затем удалите 0006 миграцию (сначала создайте резервную копию для обеспечения безопасности или убедитесь, что она находится в системе управления версиями). Наконец, повторно запустите makemigrations и выполните миграцию.