在迁移文件中创建子页面

时间:2017-11-14 14:23:45

标签: django migration django-migrations wagtail

我试图创建索引home页面的子页面,有些像这样:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-13 22:50
from __future__ import unicode_literals

from django.db import migrations


def create_blogindexpage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    BlogIndexPage = apps.get_model('blog.BlogIndexPage')
    HomePage = apps.get_model('home.HomePage')

    blogindexpage_content_type, __ = ContentType.objects.get_or_create(
        model='blogindexpage', app_label='blog')

    # Create a new blogindexpage
    blogindexpage = BlogIndexPage.objects.create(
        title="Blogs",
        draft_title="Blogs",
        slug='blogs',
        content_type=blogindexpage_content_type,
        path='000100010001',
        depth=3,
        numchild=0,
        url_path='/blogs/',
    )

    home_page = HomePage.objects.get(id=3)
    home_page.add_child(instance=blogindexpage)


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

    operations = [
        migrations.RunPython(
            create_blogindexpage,
        ),
    ]

此博客索引必须是home页面中的子项,此迁移文件取决于home create_homepage迁移,但它会给我这个错误:

File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/salahaddin/Proyectos/Works/partnerlatam/blog.xprende/xprende/blog/migrations/0002_create_blogindexpage.py", line 30, in create_blogindexpage
    home_page.add_child(instance=blogindexpage)
AttributeError: 'HomePage' object has no attribute 'add_child'

我无法理解为什么我有这个错误。我需要创建此页面并将其设为home页面中的子项,我该怎么做?

更新 我用这个解决了这个问题:

def create_blogindexpage(apps, schema_editor):
    from home.models import HomePage

    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    BlogIndexPage = apps.get_model('blog.BlogIndexPage')

    blogindexpage_content_type, __ = ContentType.objects.get_or_create(
        model='blogindexpage', app_label='blog')

    home_page = HomePage.objects.get(id=3)
    home_page.add_child(
        instance=BlogIndexPage(
            title="Blogs",
            draft_title="Blogs",
            slug='blogs',
            content_type=blogindexpage_content_type,
            numchild=0,
            url_path='/blogs/',
        )
    )

这不是正确的方法,但是,如果你能看到我的评论,我不知道如何使用move功能来做到这一点。

0 个答案:

没有答案