10

我正在尝试向我不久前创建的 ManyToMany 字段添加订单。我基本上想在图片集中订购图片。我在 Django 1.7 上运行,所以不再向南迁移(我试图按照本教程进行操作:http: //mounirmesselmeni.github.io/2013/07/28/migrate-django-manytomany-field-to-manytomany-through -与南/

这是我拥有的“通过”关系:

class CollectionPictures(models.Model):
    picture = models.ForeignKey(
        Picture,
        verbose_name=u'Picture',
        help_text=u'Picture is included in this collection.',
    )
    collection = models.ForeignKey(
        Collection,
        verbose_name=u'Collection',
        help_text=u'Picture is included in this collection',
    )
    order = models.IntegerField(
        verbose_name=u'Order',
        help_text=u'What order to display this picture within the collection.',
        max_length=255
    )

    class Meta:
        verbose_name = u"Collection Picture"
        verbose_name_plural = u"Collection Pictures"
        ordering = ['order', ]

    def __unicode__(self):
        return self.picture.name + " is displayed in " + self.collection.name + (
        " in position %d" % self.order)


class Collection(models.Model):
    pictures = models.ManyToManyField(Picture, through='CollectionPictures', null=True)
    [... Bunch of irrelevant stuff after]

因此,如果我不必迁移旧数据, 这应该可以工作(模型中唯一的区别是它没有through='CollectionPictures'

这是我的迁移:

class Migration(migrations.Migration):

    dependencies = [
        ('artist', '0002_auto_20141013_1451'),
        ('business', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='CollectionPictures',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('order', models.IntegerField(help_text='What order to display this picture within the collection.', max_length=255, verbose_name='Order')),
                ('collection', models.ForeignKey(verbose_name='Collection', to='business.Collection', help_text='Picture is included in this collection')),
                ('picture', models.ForeignKey(verbose_name='Picture', to='artist.Picture', help_text='Picture is included in this collection.')),
            ],
            options={
                'ordering': ['order'],
                'verbose_name': 'Collection Picture',
                'verbose_name_plural': 'Collection Pictures',
            },
            bases=(models.Model,),
        ),
        migrations.AlterField(
            model_name='collection',
            name='pictures',
            field=models.ManyToManyField(to=b'artist.Picture', null=True, through='business.CollectionPictures'),
        ),
    ]

迁移时会引发错误:

ValueError:无法将字段 business.Collection.pictures 更改为 business.Collection.pictures - 它们不是兼容的类型(您无法更改 M2M 字段或从 M2M 字段更改,或通过 = 在 M2M 字段上添加或删除)

是否有人已经尝试过使用新的 1.7 迁移进行这种操作?

谢谢 !

4

2 回答 2

23

最安全的方法是创建一个新字段并复制数据。

  1. 不理pictures会并添加pictures2您的through字段。运行makemigrations

  2. 编辑生成的迁移文件并添加一个RunPython命令,将数据从旧表复制到新表。order也许您也可以以编程方式为新列选择一个好的值。

  3. 删除旧pictures字段。运行makemgirations

  4. 重命名pictures2pictures. 运行makemigrations

这种方法应该让您处于您想要的状态,并且您的数据完好无损。

如果复制数据是一个大问题,您可以尝试其他方法,例如order在 SQL 中添加列,使用db_table选项 onCollectionPictures使其指向现有表,然后清除迁移并使用--fake. 但这似乎比上述方法风险更大。

于 2014-10-14T03:55:15.197 回答
5

老问题,但我也遇到了这个问题,我找到了一种适用于 Django 1.11 的方法,并且也应该适用于旧版本。需要的类存在于 1.7 并且仍然存在于 2.0

修复涉及手动更改迁移以执行我们想要的操作,使用SeparationDatabaseAndState迁移类。这个类让 Django 更新状态,但让我们控制要执行的操作。在这种情况下,我们只想重命名模型表,其他所有内容都已正确设置。

步骤:

  1. 创建新的 ManyToMany Through 模型,但指定自定义表名,并且没有额外的字段:

    class CollectionPictures(models.Model):
        collection = ...
        picture = ...
        class Meta:
            # Change myapp to match.
            db_table = "myapp_collection_pictures"
            unique_together = (("collection", "picture"))
    
  2. 采用现有的迁移,并采用它生成的操作并将其全部包装在一个新的SeparateDatabaseAndState

    class Migration(migrations.Migration):
    
        dependencies = [
            ('artist', '0002_auto_20141013_1451'),
            ('business', '0001_initial'),
        ]
    
        operations = [
            migrations.SeparateDatabaseAndState(
                database_operations=[
                ],
                state_operations=[
                    migrations.CreateModel(
                        name='CollectionPictures',
                        fields=[
                            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                            ('order', models.IntegerField(help_text='What order to display this picture within the collection.', max_length=255, verbose_name='Order')),
                            ('collection', models.ForeignKey(verbose_name='Collection', to='business.Collection', help_text='Picture is included in this collection')),
                            ('picture', models.ForeignKey(verbose_name='Picture', to='artist.Picture', help_text='Picture is included in this collection.')),
                        ],
                        options={
                            'ordering': ['order'],
                            'verbose_name': 'Collection Picture',
                            'verbose_name_plural': 'Collection Pictures',
                        },
                        bases=(models.Model,),
                    ),
                    migrations.AlterField(
                        model_name='collection',
                        name='pictures',
                        field=models.ManyToManyField(to=b'artist.Picture', null=True, through='business.CollectionPictures'),
                    ),
                ]
            )
    
  3. 从 Meta 类中删除db_table,并在 , 之后添加此操作SeparateDatabaseAndState(而不是在 database_operations 中。):

    migrations.AlterModelTable(
        name='collectionpicture',
        table=None,
    ),
    

现在,如果您运行 `./mange.py sqlmigrate myapp 0003(选择正确的数字前缀!),您应该幸运地看到如下输出:

BEGIN;
--
-- Custom state/database change combination
--
--
-- Rename table for collection[Pictures to None
--
ALTER TABLE "myapp_collection_pictures" RENAME TO "myapp_collectionpictures";
COMMIT;
  1. 添加新列(在本例中为“订单”)并创建新的迁移。可能可以同时执行此操作,但我认为在两次迁移中执行此操作更容易。

(如果您愿意将自定义表名保留在此处,则不严格要求第 3 步。)

并仔细检查./manage.py makemigrations --check- 它应该打印“未检测到更改”。

于 2018-02-04T15:17:44.453 回答