Django Sites Framework:初始数据迁移位置 [英] Django Sites Framework: Initial Data Migration Location

查看:95
本文介绍了Django Sites Framework:初始数据迁移位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django 1.7之前,使用 Django Sites框架可以/应该使用初始夹具来定义初始数据.

Before Django 1.7, when using the Django Sites Framework one could/should define the initial data using Initial Fixtures.

myproject/fixtures/initial_data.json

[
{
    "pk": 1, 
    "model": "sites.site", 
    "fields": {
        "domain": "domain1", 
        "name": "name1"
    }
},
{
    "pk": 2, 
    "model": "sites.site", 
    "fields": {
        "domain": "domain2", 
        "name": "name2"
    }
},
{
    "pk": 3, 
    "model": "sites.site", 
    "fields": {
        "domain": "domain3", 
        "name": "name3"
    }
}
]

由于它是全局项目设置,因此我在项目根目录中添加了一个"fixtures"文件夹,并将其添加到FIXTURE_DIRS.

Since it is a global project setting, I added a "fixtures" folder to the project root, and added it to FIXTURE_DIRS.

# Used to search fixture files directories.
# Fixture files are files that provide initial data to be
# inserted in the database. (>python manage.py loaddata)

    FIXTURE_DIRS = [
        os.path.join(PROJECT_ROOT, "fixtures"),
    ]

现在,我正在使用Django 1.7,建议使用

Now, I'm using Django 1.7, and it is recommended to use migrations. Quoting Django documentation:

要为项目设置正确的名称和域,可以使用数据迁移.

To set the correct name and domain for your project, you can use a data migration.

问题是迁移是特定于应用的:

python manage.py makemigrations --empty yourappname

python manage.py makemigrations --empty yourappname

那么,使用数据迁移将网站信息添加到我的项目中的推荐方法是什么?这种迁移应该住在哪里?

So, what is the recommended approach to add the Site information to my project, using a data migration? Where should this migration live?

运行python manage.py makemigrations --empty sites在第三方应用文件夹中创建迁移,因此我们不希望这样做.

Running python manage.py makemigrations --empty sites creates the migration in the third party app folder, so we don't want that.

由于初始数据已存在FIXTURE_DIRS,因此无法定义MIGRATION_DIRS吗?

Shouldn't be possible to define a MIGRATION_DIRS as FIXTURE_DIRS existed for the initial_data?

我在设置文档中找到 MIGRATION_MODULES ,但问题仍然存在,它是特定于应用程序的.

I found MIGRATION_MODULES in settings documentation, but the problem still remains, it is app-specific.

推荐答案

首先,在您的Django设置中配置MODULE_MIGRATIONS:

First, configure MODULE_MIGRATIONS in your django settings:

MIGRATION_MODULES = {
    'sites': 'myproject.fixtures.sites_migrations',
}

然后,运行./manage.py makemigrations sites让django创建目录并在myproject.fixtures.sites_migrations包中创建0001_intitial.py.

Then, run ./manage.py makemigrations sites to have django create the directory and create 0001_intitial.py in the myproject.fixtures.sites_migrations package.

然后,执行./manage.py makemigrations --empty sites.迁移文件应在指定的程序包中创建.

Then, do ./manage.py makemigrations --empty sites. The migration file should be created in the specified package.

我的文件0002_initialize_sites.py看起来像这样:

My file 0002_initialize_sites.py looks like this:

-*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations


def insert_sites(apps, schema_editor):
    """Populate the sites model"""
    Site = apps.get_model('sites', 'Site')
    Site.objects.all().delete()

    # Register SITE_ID = 1
    Site.objects.create(domain='create.tourtodo.com', name='create')
    # Register SITE_ID = 2
    Site.objects.create(domain='www.tourtodo.com', name='www')


class Migration(migrations.Migration):

    dependencies = [
        ('sites', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(insert_sites)
    ]

这篇关于Django Sites Framework:初始数据迁移位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆