jej*_*343 9 django django-models django-orm
我正在尝试为我的Django项目运行迁移,但是我收到错误:
AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name'
Run Code Online (Sandbox Code Playgroud)
当我在所有应用程序上运行迁移时,我没有收到任何错误.只有当我尝试实际迁移时才会这样.我无法从追溯信息中了解哪个模型正在创建问题,甚至是哪个应用程序.我看过我的模特,我看不到任何突然出现的东西.
这是堆栈跟踪:
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'
Run Code Online (Sandbox Code Playgroud)
我如何找出问题所在的模型?我应该寻找什么?
您必须确保已在数据库中创建了创建"ManyToManyField"的模型.
您可以通过将创建模型的迁移作为依赖关系添加到您在其中更改字段的迁移中来实现:
场景1:使用其他应用程序中的模型将字段更改为"ManyToManyField"
class Migration(migrations.Migration):
dependencies = [
..........
('[app]', '__first__'),
]
operations = [
.........
]
Run Code Online (Sandbox Code Playgroud)
场景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(...)
]
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,但由于相同的原因我不知道。幸运的是,我在系统中没有任何重要数据,因此我按如下所示更改了迁移- 但请注意,这会删除这些列中的所有数据!
之前:
operations = [
migrations.AlterField(
model_name='resource',
name='authors',
field=models.ManyToManyField(related_name='resources_authored', to='api.Person'),
),
migrations.AlterField(
model_name='resource',
name='editors',
field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'),
),
]
Run Code Online (Sandbox Code Playgroud)
后:
operations = [
migrations.RemoveField(
model_name='resource',
name='authors',
),
migrations.RemoveField(
model_name='resource',
name='editors',
),
migrations.AddField(
model_name='resource',
name='authors',
field=models.ManyToManyField(related_name='resources_authored', to='api.Person'),
),
migrations.AddField(
model_name='resource',
name='editors',
field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'),
),
]
Run Code Online (Sandbox Code Playgroud)
尽管更改由于神秘原因而失败,但删除并重新创建字段仍然有效。
归档时间: |
|
查看次数: |
5471 次 |
最近记录: |