Python Django数据迁移顺序
我正在我的应用程序中使用sites应用程序(Python Django数据迁移顺序,python,django,django-migrations,Python,Django,Django Migrations,我正在我的应用程序中使用sites应用程序(django.contrib.sites)。我创建了一个数据迁移,在创建数据库时设置当前站点的值,但是我的数据迁移是在安装站点应用程序之前执行的。如何在站点迁移后强制执行数据迁移 该项目旨在成为用于其他项目的种子,我经常删除数据库并重新开始,因此初始的makemigrations/migrate命令开箱即用非常重要 我的迁移文件存在于主应用程序文件夹中: project folder ..+app ....+migrations ......-0001
django.contrib.sites
)。我创建了一个数据迁移,在创建数据库时设置当前站点的值,但是我的数据迁移是在安装站点应用程序之前执行的。如何在站点
迁移后强制执行数据迁移
该项目旨在成为用于其他项目的种子,我经常删除数据库并重新开始,因此初始的makemigrations/migrate命令开箱即用非常重要
我的迁移文件存在于主应用程序文件夹中:
project folder
..+app
....+migrations
......-0001_initial.py
以下是迁移文件的内容:
from __future__ import unicode_literals
from django.db import migrations
def set_development_site(apps, schema_editor):
Site = apps.get_model('sites', 'Site')
current= Site.objects.get_current()
current.domain = "localhost:8000"
current.name = "Django-Angular-Webpack-Starter"
current.save()
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.RunPython(set_development_site),
]
dependencies = [
('sites', '0001_initial'),
...
]
以及python manage.py migrate
命令的输出:
Operations to perform:
Apply all migrations: admin, app, auth, authentication, authtoken, contenttypes, sessions, sites
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying authentication.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying app.0001_initial...Traceback (most recent call last):
File "/home/user/devel/django-angular-webpack-starter/venv/lib/python3.5/site-packages/django/apps/registry.py", line 149, in get_app_config
return self.app_configs[app_label]
KeyError: 'sites'
默认情况下,未启用(迁移)站点框架。这就是为什么在站点迁移之前不能引用
站点
模型的原因。您必须先安装并迁移它。您要执行:manage.py迁移站点
和manage.py迁移
更新
如果只想使用manage.py migrate
尝试将站点
作为依赖项添加到迁移文件中:
from __future__ import unicode_literals
from django.db import migrations
def set_development_site(apps, schema_editor):
Site = apps.get_model('sites', 'Site')
current= Site.objects.get_current()
current.domain = "localhost:8000"
current.name = "Django-Angular-Webpack-Starter"
current.save()
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.RunPython(set_development_site),
]
dependencies = [
('sites', '0001_initial'),
...
]
默认情况下,未启用(迁移)相关文档文章站点框架。这就是为什么在站点迁移之前不能引用
站点
模型的原因。您必须先安装并迁移它。您要执行:manage.py迁移站点
和manage.py迁移
更新
如果只想使用manage.py migrate
尝试将站点
作为依赖项添加到迁移文件中:
from __future__ import unicode_literals
from django.db import migrations
def set_development_site(apps, schema_editor):
Site = apps.get_model('sites', 'Site')
current= Site.objects.get_current()
current.domain = "localhost:8000"
current.name = "Django-Angular-Webpack-Starter"
current.save()
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.RunPython(set_development_site),
]
dependencies = [
('sites', '0001_initial'),
...
]
相关文档文章python manage.py站点。在为app.python manage.py站点启动命令之后。在启动应用程序的命令后。好的,可以了,不过我真的很想在访问模型之前强制站点应用程序迁移,这样最终用户在从我的库启动新应用程序时就可以像正常情况一样运行
python manage.py migrate
,一切正常。更新了我的答案。好的,更新后的解决方案有效-对我来说只有一个小改动:站点迁移运行后站点不存在,您只需使用current=site.objects.create(domain=…',name=…)
而不是current=site.objects.get_current()
,创建站点即可,虽然我真的很想在访问模型之前强制sites应用程序迁移,这样我的最终用户在从我的库启动新应用程序时就可以像正常情况一样运行python manage.py migrate
,一切正常。更新了我的答案。好的,更新后的解决方案有效-对我来说只有一个小改动:站点迁移运行后站点不存在,您只需使用current=site.objects.create(domain=…',name=…)
而不是current=site.objects.get_current()
创建站点。