我正在尝试为我的 Django 项目运行迁移,但出现错误:

AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name'

当我在所有应用程序上运行 make migrations 时,我没有收到任何错误。只有当我尝试真正迁移时。我无法从回溯信息中分辨出哪个模型产生了问题,甚至是哪个应用程序。我看过我的模型,我没有看到任何东西突然出现在我身上。

这是堆栈跟踪:
Operations to perform:
  Apply all migrations: admin, sessions, case_manager, file_manager, auth, contenttypes, tasks, people_and_property
Running migrations:
  Rendering model states... DONE
  Applying file_manager.0006_auto_20160109_1536...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-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 "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 467, in alter_field
    return self._alter_many_to_many(model, old_field, new_field, strict)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 274, in _alter_many_to_many
    old_field.remote_field.through._meta.get_field(old_field.m2m_reverse_field_name()),
AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name'

我如何确定哪个型号有问题?我应该寻找什么?

最佳答案

您必须确保您正在创建的模型“ManyToManyField”已在数据库中创建。

您可以通过将创建模型的迁移作为依赖项添加到您更改字段的迁移来实现:

场景 1:您使用来自其他应用程序的模型将字段更改为“ManyToManyField”

class Migration(migrations.Migration):

    dependencies = [
      ..........
      ('[app]', '__first__'),
    ]

    operations = [
       .........
    ]

场景 2:您创建了一个“ManyToManyField”并且您所指的模型在同一个文件中:
 class Migration(migrations.Migration):

    dependencies = [
      ..........
    ]

    operations = [
       .........
       # Make sure the model you are making the reference with is  before the ManyToManyField
       migrations.CreateModel(...) ,
       migrations.AlterField/CreateField(...)

    ]

关于django - 'ManyToManyField' 对象没有属性 'm2m_reverse_field_name',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34698845/

10-15 08:53