迁移文件不更改站点名称和域
Migration file don't change Site name and domain
我有这个迁移文件:
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
迁移作为此迁移文件的依赖项,但它仍然不起作用。
谁能解释我为什么会出现这个错误?
谢谢。
您必须 运行 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()
我有这个迁移文件:
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
迁移作为此迁移文件的依赖项,但它仍然不起作用。
谁能解释我为什么会出现这个错误?
谢谢。
您必须 运行 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()