在迁移中创建集合

Create collections inside migrations

我想为应用一个迁移的创建集合创建迁移。

自 python 代码以来,我还不清楚创建 集合 的正确工作流程,有人可以帮助我吗?

这里提到的东西有用吗?

python manage.py squashmigrations <appname> <squashfrom> <squashto>

此致

好的,在网上搜索我找到了两个解决方案: google mailing list, and other here in Whosebug.

中的一个

测试 shell 中的代码后,我们可以看到它适用于创建集合:

from wagtail.wagtailcore.models import Collection
root_coll = Collection.get_first_root_node()
root_coll.add_child(name='testcoll')

现在我们可以在迁移中使用此代码,因此,使用 ./manage.py makemigrations home --empty 创建一个新的空迁移并在之前的代码中添加:

from __future__ import unicode_literals

from django.db import migrations

from wagtail.wagtailcore.models import Collection


def create_collections(apps, schema_editor):
    names = [
        'video tutoriales',
        'videos personal',
        'videos varios',
        'imagenes tutoriales',
        'imagenes personal',
        'imaganes varias',
        'documentos tutoriales',
        'documentos personal',
        'documentos varios'
    ]
    # Get models

    # Get collection's root
    root_collection = Collection.get_first_root_node()

    for name in names:
        root_collection.add_child(name=name)


class Migration(migrations.Migration):
    dependencies = [
        ('home', '0002_create_homepage'),
    ]

    operations = [
        migrations.RunPython(create_collections),
    ]

首先,我们在变量中获取主 Collection 节点,然后添加任意数量的子节点及其各自的名称。

实际上是一个非常基本的代码。

谁能优化一下,欢迎,谢谢