Django迁移错误:您不能在M2M字段之间来回更改,也不能在M2M字段上添加through =或从中删除 [英] 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字段上添加through =或从中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!