【问题标题】:Auto Fake Django Migration自动伪造 Django 迁移
【发布时间】:2018-08-15 11:22:47
【问题描述】:

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

迁移布局是这样的

migtarions
    |-- 0001_initial.py
    |-- 0002.py
    |-- 0003.py

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

【问题讨论】:

  • 请注意,Django 1.9 已结束生命周期,并且不会收到安全更新。您至少应该尝试升级到 1.11.x LTS 版本。
  • 您可以删除MigrationMigration 中的operations 0002.py 并将它们替换为不执行任何操作的虚拟文件(或删除文件并更正dependencies) .
  • 也许我应该更清楚我需要做什么。我有一个现有的表,它是第三方应用程序的一部分。我正在重写模型,更改了一些字段。所以 0002.py 使用引用现有表的元创建新模型。 0003.py 然后进行字段更新。
  • 您需要删除 0002.py 迁移文件并再次运行迁移命令。

标签: python django migration


【解决方案1】:

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

./manage.py migrate --fake-initial

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

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

有关更多信息,请参阅initial migrations 上的文档。

【讨论】:

    【解决方案2】:

    从 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 2017-05-14
      • 1970-01-01
      • 2021-12-23
      • 2019-06-17
      • 2018-08-24
      • 2015-06-09
      • 2021-01-09
      相关资源
      最近更新 更多