Django 迁移错误:您无法更改 M2M 字段或从 M2M 字段更改,或通过 = 在 M2M 字段上添加或删除 [英] Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields
问题描述
我正在尝试将 M2M 字段修改为 ForeignKey 字段.命令 validate 显示没有问题,当我运行 syncdb 时:
I'm trying to modify a M2M field to a ForeignKey field. The command validate shows me no issues and when I run syncdb :
ValueError: Cannot alter field xxx into yyy they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
所以我无法进行迁移.
class InstituteStaff(Person):
user = models.OneToOneField(User, blank=True, null=True)
investigation_area = models.ManyToManyField(InvestigationArea, blank=True,)
investigation_group = models.ManyToManyField(InvestigationGroup, blank=True)
council_group = models.ForeignKey(CouncilGroup, null=True, blank=True)
#profiles = models.ManyToManyField(Profiles, null = True, blank = True)
profiles = models.ForeignKey(Profiles, null = True, blank = True)
这是我的第一个 Django 项目,欢迎提出任何建议.
This is my first Django project so any suggestions are welcome.
推荐答案
我偶然发现了这个,虽然我不太关心我的数据,但我仍然不想删除整个数据库.因此,我打开了迁移文件并将 AlterField()
命令更改为运行良好的 RemoveField()
和 AddField()
命令.我丢失了我在特定领域的数据,但没有别的.
I stumbled upon this and although I didn't care about my data much, I still didn't want to delete the whole DB. So I opened the migration file and changed the AlterField()
command to a RemoveField()
and an AddField()
command that worked well. I lost my data on the specific field, but nothing else.
即
migrations.AlterField(
model_name='player',
name='teams',
field=models.ManyToManyField(related_name='players', through='players.TeamPlayer', to='players.Team'),
),
到
migrations.RemoveField(
model_name='player',
name='teams',
),
migrations.AddField(
model_name='player',
name='teams',
field=models.ManyToManyField(related_name='players', through='players.TeamPlayer', to='players.Team'),
),
这篇关于Django 迁移错误:您无法更改 M2M 字段或从 M2M 字段更改,或通过 = 在 M2M 字段上添加或删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!