0

I want create migrations for create collections applying one migration.

I haven't clear which is the properly workflow for create collections since python code, can anyone help to me?

SalahAdDin
  • 2,023
  • 25
  • 51

2 Answers2

0

Ok, searching in the web i found two solutions: one in google mailing list, and other here in StackOverflow.

After test the code in the shell we can see that it works for create collections:

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

Now we can use this code but in a migration, so, create a new empty migration with ./manage.py makemigrations home --empty and add inside the previous code:

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),
    ]

First, we get the main Collection node in a variable, after we add whatever amount of child with his respective name.

Is a very basic code actually.

If anyone can optimize it, your welcome, thank you.

Community
  • 1
  • 1
SalahAdDin
  • 2,023
  • 25
  • 51
-1

Does something as mentioned here would work?

How to squash recent Django migrations?

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

Regards

Community
  • 1
  • 1
mouch
  • 37
  • 3
  • 1
    Consider explaining or elaborating your answer so it can help other users, it is very unclear how it solves the problem – Chidi Ekuma May 01 '17 at 00:38