且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django 迁移错误:您无法更改 M2M 字段或从 M2M 字段更改,或通过 = 在 M2M 字段上添加或删除

更新时间:2021-08-21 23:36:29

我偶然发现了这个,虽然我不太关心我的数据,但我仍然不想删除整个数据库.因此,我打开了迁移文件并将 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'),
),