自动假Django迁移

时间:2018-03-07 11:13:57

标签: python django migration

我需要自动伪造Django 1.9迁移。是否有可以放入迁移文件的标志,该文件将自动伪造,而不必登录到多个服务器并运行。

迁移布局就像这样

asset(str_replace(app_path(),'',$img->path))

我知道我可以做./manage.py my_app migrate 0002 --fake,但迁移0002可以自动伪造吗?我需要运行的是./manage.py my_app migrate

2 个答案:

答案 0 :(得分:0)

Django 1.8+不会自动伪造迁移。您需要使用--fake-initial选项告诉Django伪造初始迁移。

./manage.py migrate --fake-initial

然后,您可以在initial = True迁移类上设置0002,告诉Django这是初始迁移。

class Migration(migrations.Migration):
    initial = True    
    dependencies = [('migrations', '0001_initial')]

有关详细信息,请参阅initial migrations上的文档。

答案 1 :(得分:0)

在具有现有迁移的实时项目中,从Django的本机用户模型迁移到自定义模型时,我需要做同样的事情。我通过覆盖apply / unapply方法来实现它:

# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2018-08-13 13:43
from __future__ import unicode_literals

import django.contrib.auth.models
import django.core.validators
from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

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

    operations = [
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('password', models.CharField(max_length=128, verbose_name='password')),
                ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
                ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
                ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 255 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=255, unique=True, validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.')], verbose_name='username')),
                ('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
                ('last_name', models.CharField(blank=True, max_length=30, verbose_name='last name')),
                ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
                ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
                ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
                ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
                ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
                ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
            ],
            options={
                'db_table': 'auth_user',
            },
            managers=[
                ('objects', django.contrib.auth.models.UserManager()),
            ],
        ),
    ]

    def apply(self, project_state, schema_editor, collect_sql=False):
        return project_state

    def unapply(self, project_state, schema_editor, collect_sql=False):
        return project_state