Python django.db.migrations.AddField() Examples

The following are code examples for showing how to use django.db.migrations.AddField(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: django-rest-passwordreset   Author: anexia-it   File: 0002_pk_migration.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_migrations_for_django_21_and_newer():
    return [
        # remove primary key information from 'key' field
        migrations.AlterField(
            model_name='resetpasswordtoken',
            name='key',
            field=models.CharField(db_index=True, primary_key=False, max_length=64, unique=True, verbose_name='Key'),
        ),
        # add a new id field
        migrations.AddField(
            model_name='resetpasswordtoken',
            name='id',
            field=models.AutoField(primary_key=True, serialize=False),
            preserve_default=False,
        ),
        migrations.RunPython(
            populate_auto_incrementing_pk_field,
            migrations.RunPython.noop
        ),
    ] 
Example 2
Project: mp-novaposhta   Author: pmaigutyak   File: 0002_model_translation.py    ISC License 6 votes vote down vote up
def generate_operations():

    operations = []

    for code, name in settings.LANGUAGES:

        operations += [

            migrations.AddField(
                model_name='warehouse',
                name='address_%s' % code,
                field=models.CharField(db_index=True, max_length=255, null=True, verbose_name='Address'),
            ),
            migrations.AddField(
                model_name='warehouse',
                name='title_%s' % code,
                field=models.CharField(db_index=True, max_length=255, null=True, verbose_name='Title'),
            ),

        ]

        return operations 
Example 3
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 4
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 5
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.name.lower():
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 6
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name.lower() == other.model_name.lower() and
                operation.name.lower() == other.old_name.lower()):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 7
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def can_optimize_through(self, operation, other, app_label=None):
        """
        Returns True if it's possible to optimize 'operation' with something
        the other side of 'other'. This is possible if, for example, they
        affect different models.
        """
        MODEL_LEVEL_OPERATIONS = (
            migrations.CreateModel,
            migrations.AlterModelTable,
            migrations.AlterUniqueTogether,
            migrations.AlterIndexTogether,
        )
        FIELD_LEVEL_OPERATIONS = (
            migrations.AddField,
            migrations.AlterField,
        )
        # If it's a model level operation, let it through if there's
        # nothing that looks like a reference to us in 'other'.
        if isinstance(operation, MODEL_LEVEL_OPERATIONS):
            if not other.references_model(operation.name, app_label):
                return True
        # If it's field level, only let it through things that don't reference
        # the field (which includes not referencing the model)
        if isinstance(operation, FIELD_LEVEL_OPERATIONS):
            if not other.references_field(operation.model_name, operation.name, app_label):
                return True
        return False 
Example 8
Project: AneMo   Author: jspargo   File: test_operations.py    GNU General Public License v2.0 5 votes vote down vote up
def test_add_gis_field(self):
        """
        Tests the AddField operation with a GIS-enabled column.
        """
        project_state = self.set_up_test_model()
        self.current_state = project_state
        operation = migrations.AddField(
            "Neighborhood",
            "path",
            fields.LineStringField(srid=4326),
        )
        new_state = project_state.clone()
        operation.state_forwards("gis", new_state)
        with connection.schema_editor() as editor:
            operation.database_forwards("gis", editor, project_state, new_state)
        self.current_state = new_state
        self.assertColumnExists("gis_neighborhood", "path")

        # Test GeometryColumns when available
        if HAS_GEOMETRY_COLUMNS:
            self.assertGeometryColumnsCount(2)

        if self.has_spatial_indexes:
            with connection.cursor() as cursor:
                indexes = connection.introspection.get_indexes(cursor, "gis_neighborhood")
            self.assertIn('path', indexes) 
Example 9
Project: AneMo   Author: jspargo   File: test_operations.py    GNU General Public License v2.0 5 votes vote down vote up
def test_add_blank_gis_field(self):
        """
        Should be able to add a GeometryField with blank=True.
        """
        project_state = self.set_up_test_model()
        self.current_state = project_state
        operation = migrations.AddField(
            "Neighborhood",
            "path",
            fields.LineStringField(blank=True, srid=4326),
        )
        new_state = project_state.clone()
        operation.state_forwards("gis", new_state)
        with connection.schema_editor() as editor:
            operation.database_forwards("gis", editor, project_state, new_state)
        self.current_state = new_state
        self.assertColumnExists("gis_neighborhood", "path")

        # Test GeometryColumns when available
        if HAS_GEOMETRY_COLUMNS:
            self.assertGeometryColumnsCount(2)

        if self.has_spatial_indexes:
            with connection.cursor() as cursor:
                indexes = connection.introspection.get_indexes(cursor, "gis_neighborhood")
            self.assertIn('path', indexes) 
Example 10
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 11
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 12
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 13
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 14
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 15
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 16
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 17
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 18
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 19
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 20
Project: django-rest-passwordreset   Author: anexia-it   File: 0002_pk_migration.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_migrations_for_django_before_21():
    return [
        # add a new id field (without primary key information)
        migrations.AddField(
            model_name='resetpasswordtoken',
            name='id',
            field=models.IntegerField(null=True),
            preserve_default=True,
        ),
        # fill the new pk field
        migrations.RunPython(
            populate_auto_incrementing_pk_field,
            migrations.RunPython.noop
        ),
        # add primary key information to id field
        migrations.AlterField(
            model_name='resetpasswordtoken',
            name='id',
            field=models.AutoField(primary_key=True, serialize=False)
        ),
        # remove primary key information from 'key' field
        migrations.AlterField(
            model_name='resetpasswordtoken',
            name='key',
            field=models.CharField(db_index=True, max_length=64, unique=True, verbose_name='Key'),
        ),
    ] 
Example 21
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 22
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 23
Project: wagtail-translation   Author: skirsdeda   File: 9999_wagtail_translation.py    MIT License 5 votes vote down vote up
def get_add_field_for_langs(name, **kwargs):
    ret = []

    for lang_code in mt_settings.AVAILABLE_LANGUAGES:
        trans_field = build_localized_fieldname(name, lang_code)
        ret.append(migrations.AddField(name=trans_field, **kwargs))

    return ret 
Example 24
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.name.lower():
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 25
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name.lower() == other.model_name.lower() and
                operation.name.lower() == other.old_name.lower()):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 26
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def can_optimize_through(self, operation, other, app_label=None):
        """
        Returns True if it's possible to optimize 'operation' with something
        the other side of 'other'. This is possible if, for example, they
        affect different models.
        """
        MODEL_LEVEL_OPERATIONS = (
            migrations.CreateModel,
            migrations.AlterModelTable,
            migrations.AlterUniqueTogether,
            migrations.AlterIndexTogether,
        )
        FIELD_LEVEL_OPERATIONS = (
            migrations.AddField,
            migrations.AlterField,
        )
        # If it's a model level operation, let it through if there's
        # nothing that looks like a reference to us in 'other'.
        if isinstance(operation, MODEL_LEVEL_OPERATIONS):
            if not other.references_model(operation.name, app_label):
                return True
        # If it's field level, only let it through things that don't reference
        # the field (which includes not referencing the model)
        if isinstance(operation, FIELD_LEVEL_OPERATIONS):
            if not other.references_field(operation.model_name, operation.name, app_label):
                return True
        return False 
Example 27
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: test_operations.py    GNU General Public License v2.0 5 votes vote down vote up
def test_add_gis_field(self):
        """
        Tests the AddField operation with a GIS-enabled column.
        """
        project_state = self.set_up_test_model()
        self.current_state = project_state
        operation = migrations.AddField(
            "Neighborhood",
            "path",
            fields.LineStringField(srid=4326),
        )
        new_state = project_state.clone()
        operation.state_forwards("gis", new_state)
        with connection.schema_editor() as editor:
            operation.database_forwards("gis", editor, project_state, new_state)
        self.current_state = new_state
        self.assertColumnExists("gis_neighborhood", "path")

        # Test GeometryColumns when available
        if HAS_GEOMETRY_COLUMNS:
            self.assertGeometryColumnsCount(2)

        if self.has_spatial_indexes:
            with connection.cursor() as cursor:
                indexes = connection.introspection.get_indexes(cursor, "gis_neighborhood")
            self.assertIn('path', indexes) 
Example 28
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: test_operations.py    GNU General Public License v2.0 5 votes vote down vote up
def test_add_blank_gis_field(self):
        """
        Should be able to add a GeometryField with blank=True.
        """
        project_state = self.set_up_test_model()
        self.current_state = project_state
        operation = migrations.AddField(
            "Neighborhood",
            "path",
            fields.LineStringField(blank=True, srid=4326),
        )
        new_state = project_state.clone()
        operation.state_forwards("gis", new_state)
        with connection.schema_editor() as editor:
            operation.database_forwards("gis", editor, project_state, new_state)
        self.current_state = new_state
        self.assertColumnExists("gis_neighborhood", "path")

        # Test GeometryColumns when available
        if HAS_GEOMETRY_COLUMNS:
            self.assertGeometryColumnsCount(2)

        if self.has_spatial_indexes:
            with connection.cursor() as cursor:
                indexes = connection.introspection.get_indexes(cursor, "gis_neighborhood")
            self.assertIn('path', indexes) 
Example 29
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 30
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 31
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.name.lower():
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 32
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.old_name.lower():
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 33
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def can_optimize_through(self, operation, other, app_label=None):
        """
        Returns True if it's possible to optimize 'operation' with something
        the other side of 'other'. This is possible if, for example, they
        affect different models.
        """
        MODEL_LEVEL_OPERATIONS = (
            migrations.CreateModel,
            migrations.AlterModelTable,
            migrations.AlterUniqueTogether,
            migrations.AlterIndexTogether,
        )
        FIELD_LEVEL_OPERATIONS = (
            migrations.AddField,
            migrations.AlterField,
        )
        # If it's a model level operation, let it through if there's
        # nothing that looks like a reference to us in 'other'.
        if isinstance(operation, MODEL_LEVEL_OPERATIONS):
            if not other.references_model(operation.name, app_label):
                return True
        # If it's field level, only let it through things that don't reference
        # the field (which includes not referencing the model)
        if isinstance(operation, FIELD_LEVEL_OPERATIONS):
            if not other.references_field(operation.model_name, operation.name, app_label):
                return True
        return False 
Example 34
Project: django-wechat-api   Author: crazy-canux   File: optimizer.py    GNU General Public License v3.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 35
Project: django-wechat-api   Author: crazy-canux   File: optimizer.py    GNU General Public License v3.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 36
Project: MulticsSH   Author: MulticsYin   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_add_field_alter_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=operation.name,
                    field=other.field,
                )
            ] 
Example 37
Project: MulticsSH   Author: MulticsYin   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_add_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                migrations.AddField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                )
            ] 
Example 38
Project: openhgsenti   Author: drexly   File: executor.py    Apache License 2.0 4 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables or columns it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel and AddField).
        """
        if migration.initial is None:
            # Bail if the migration isn't the first one in its app
            if any(app == migration.app_label for app, name in migration.dependencies):
                return False, project_state
        elif migration.initial is False:
            # Bail if it's NOT an initial migration
            return False, project_state

        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_model_migration = False
        found_add_field_migration = False
        # Make sure all create model and add field operations are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_model_migration = True
            elif isinstance(operation, migrations.AddField):
                model = apps.get_model(migration.app_label, operation.model_name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue

                table = model._meta.db_table
                db_field = model._meta.get_field(operation.name).column
                fields = self.connection.introspection.get_table_description(self.connection.cursor(), table)
                if db_field not in (f.name for f in fields):
                    return False, project_state
                found_add_field_migration = True
        # If we get this far and we found at least one CreateModel or AddField migration,
        # the migration is considered implicitly applied.
        return (found_create_model_migration or found_add_field_migration), after_state 
Example 39
Project: wagtail-translation   Author: skirsdeda   File: 9999_wagtail_translation.py    MIT License 4 votes vote down vote up
def __init__(self, name, app_label):
        # by changing app_label here to 'wagtailcore' we trick Django migrations system
        # to think that this migration belongs to wagtailcore app
        # this is necessary to make model name resolution work
        app_label = 'wagtailcore'
        super(Migration, self).__init__(name, app_label)

        # find last wagtailcore migration
        mod_name  = MigrationLoader.migrations_module(app_label)
        if DJANGO_VERSION >= (1, 11):
            # Django 1.11 returns tuple(str, bool) while older versions return str
            mod_name = mod_name[0]
        mod = import_module(mod_name)
        migrations = []
        # this loop acts the same way as MigrationLoader.
        for name in os.listdir(os.path.dirname(mod.__file__)):
            if not name.endswith('.py'):
                continue
            import_name = name.rsplit('.', 1)[0]
            if import_name[0] in '_.~':
                continue
            migrations.append(import_name)
        last_migration = sorted(migrations, reverse=True)[0]
        # By using `replaces` we make sure that this migration doesn't have ambiguous `app_label`.
        # When this migration is applied Django writes only replaced migration
        # to django_migrations table in DB. Otherwise migration would have
        # 'wagtailtranslation' as app_label in django_migrations table and
        # `migrate` command would consider this migration as unapplied due
        # to app_label mismatch.
        self.replaces = [
            (app_label, last_migration),
        ]

        # import operations from wagtail migration we are replacing
        # and prepend them to operations of this migration
        mod_path = '{}.{}'.format(mod_name, last_migration)
        orig_migration = import_module(mod_path).Migration
        self.operations[:0] = orig_migration.operations
        self.dependencies = orig_migration.dependencies

    # Dynamically define AddField operations for all Page field translations.
    # This always uses current AVAILABLE_LANGUAGES setting.
    # In case languages are changed after running this migration, `makemigrations`
    # command would do nothing for Page model. One would have to run `sync_translation_fields`
    # command from modeltranslation to get DB schema in sync. 
Example 40
Project: open-cabinet   Author: CivicTechTO   File: executor.py    MIT License 4 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables or columns it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel and AddField).
        """
        if migration.initial is None:
            # Bail if the migration isn't the first one in its app
            if any(app == migration.app_label for app, name in migration.dependencies):
                return False, project_state
        elif migration.initial is False:
            # Bail if it's NOT an initial migration
            return False, project_state

        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_model_migration = False
        found_add_field_migration = False
        # Make sure all create model and add field operations are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_model_migration = True
            elif isinstance(operation, migrations.AddField):
                model = apps.get_model(migration.app_label, operation.model_name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue

                table = model._meta.db_table
                db_field = model._meta.get_field(operation.name).column
                fields = self.connection.introspection.get_table_description(self.connection.cursor(), table)
                if db_field not in (f.name for f in fields):
                    return False, project_state
                found_add_field_migration = True
        # If we get this far and we found at least one CreateModel or AddField migration,
        # the migration is considered implicitly applied.
        return (found_create_model_migration or found_add_field_migration), after_state