迁移文件不会更改站点名称和域

时间:2017-11-14 14:12:59

标签: django migration sites django-sites

我有这个迁移文件:

from django.db import migrations
from django.contrib.sites.models import Site as DjangoSite


def change_django_site(apps, schema_editor):
    site = DjangoSite.objects.get(id=1)
    site.delete()

    DjangoSite.objects.get_or_create(id=1, domain='www.xprende.com', name='XPrende')


def create_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    Page = apps.get_model('wagtailcore.Page')
    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    # If migration is run multiple times, it may have already been deleted
    Page.objects.filter(id=2).delete()

    # Create content type for homepage model
    homepage_content_type, __ = ContentType.objects.get_or_create(
        model='homepage', app_label='home')

    # Create a new homepage
    homepage = HomePage.objects.create(
        title="Home",
        draft_title="Home",
        slug='home',
        content_type=homepage_content_type,
        path='00010001',
        depth=2,
        numchild=0,
        url_path='/home/',
    )

    # Create a site with the new homepage set as the root
    Site.objects.create(
        hostname='www.xprende.com', site_name='XPrende', root_page=homepage, is_default_site=True
    )


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

    # Delete the default homepage
    # Page and Site objects CASCADE
    HomePage.objects.filter(slug='home', depth=2).delete()

    # Delete content type for homepage model
    ContentType.objects.filter(model='homepage', app_label='home').delete()


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

    operations = [
        migrations.RunPython(
            create_homepage,
            remove_homepage,
            change_django_site,
        ),
    ]

正如您所看到的,我创建了一个必须更改django站点名称和域名的功能。但它没有这样做,应用迁移后,我得到相同的默认值:

In [3]: DjangoSite.objects.get(id=1)
Out[3]: <Site: example.com>

In [4]: DjangoSite.objects.get(id=1).name
Out[4]: 'example.com'

In [5]: DjangoSite.objects.get(id=1).domain
Out[5]: 'example.com'

In [6]: DjangoSite.objects.all()
Out[6]: <QuerySet [<Site: example.com>]>

我无法理解为什么,我将sites迁移作为此迁移文件的依赖项,但它仍无效。

任何人都可以解释为什么我有这个错误吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您必须将change_django_site作为单独的操作运行:

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

    operations = [
        migrations.RunPython(change_django_site, reverse_code=migrations.RunPython.noop),
        migrations.RunPython(create_homepage, reverse_code=remove_homepage),
    ]

change_django_site必须

def change_django_site(apps, schema_editor): 
    DjangoSite = apps.get_model('sites', 'Site') 
    site = DjangoSite.objects.get_or_create(id=1)
    site.domain='www.xprende.com'
    site.name='XPrende'
    site.save()