Python django.db.migrations 模块,AlterField() 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用django.db.migrations.AlterField()

项目:django-boardinghouse    作者:schinckel    | 项目源码 | 文件源码
def test_add_check_constraint(self):
        project_state = self.set_up_test_model()
        operation = migrations.AlterField(
            model_name='pony',
            name='pink',
            field=models.PositiveIntegerField(default=3)
        )
        new_state = project_state.clone()
        operation.state_forwards('tests', new_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'check')

        with connection.schema_editor() as editor:
            operation.database_forwards('tests', editor, project_state, new_state)

        self.assertConstraint('tests_pony', ['pink'], 'check')

        with connection.schema_editor() as editor:
            operation.database_backwards('tests', editor, new_state, project_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'check')
项目:django-boardinghouse    作者:schinckel    | 项目源码 | 文件源码
def test_add_unique_constraint(self):
        project_state = self.set_up_test_model()
        operation = migrations.AlterField(
            model_name='pony',
            name='pink',
            field=models.IntegerField(unique=True, default=3)
        )
        new_state = project_state.clone()
        operation.state_forwards('tests', new_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'unique')

        with connection.schema_editor() as editor:
            operation.database_forwards('tests', editor, project_state, new_state)

        self.assertConstraint('tests_pony', ['pink'], 'unique')

        with connection.schema_editor() as editor:
            operation.database_backwards('tests', editor, new_state, project_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'unique')
项目:django-postgres-extra    作者:SectorLabs    | 项目源码 | 文件源码
def test_uniqueness():
    """Tests whether changes in the `uniqueness`
    option are properly detected by the auto detector."""

    before = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField())
        ])
    ]
    after = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField(uniqueness=['en']))
        ])
    ]

    changes = detect_changes(before, after)

    assert_autodetector(changes, [
        migrations.AlterField(
            'Model1',
            'title',
            HStoreField(uniqueness=['en'])
        )
    ])
项目:django-postgres-extra    作者:SectorLabs    | 项目源码 | 文件源码
def test_required():
    """Tests whether changes in the `required`
    option are properly detected by the auto detector."""

    before = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField())
        ])
    ]
    after = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField(required=['en']))
        ])
    ]

    changes = detect_changes(before, after)

    assert_autodetector(changes, [
        migrations.AlterField(
            'Model1',
            'title',
            HStoreField(required=['en'])
        )
    ])
项目:django-boardinghouse    作者:schinckel    | 项目源码 | 文件源码
def test_alter_field(self):
        project_state = self.set_up_test_model()
        operation = migrations.AlterField('Pony', 'pink', models.IntegerField(null=True))
        new_state = project_state.clone()
        operation.state_forwards('tests', new_state)
        self.assertColumnNotNull('tests_pony', 'pink')
        with connection.schema_editor() as editor:
            operation.database_forwards('tests', editor, project_state, new_state)
        self.assertColumnNull('tests_pony', 'pink')
        with connection.schema_editor() as editor:
            operation.database_backwards('tests', editor, new_state, project_state)
        self.assertColumnNotNull('tests_pony', 'pink')
项目:django-postgres-extra    作者:SectorLabs    | 项目源码 | 文件源码
def alter_field(old_field, new_field, filters: List[str]):
    """Alters a field from one state to the other.

    Arguments:
        old_field:
            The field before altering it.

        new_field:
            The field after altering it.

        filters:
            List of strings to filter
            SQL statements on.
    """

    model = define_fake_model({'title': old_field})
    project = migrations.state.ProjectState.from_apps(apps)

    with connection.schema_editor() as schema_editor:
        execute_migration(schema_editor, [
            migrations.CreateModel(
                model.__name__,
                fields=[
                    ('title', old_field.clone())
                ]
            )
        ], project)

    with filtered_schema_editor(*filters) as (schema_editor, calls):
        execute_migration(schema_editor, [
            migrations.AlterField(
                model.__name__,
                'title',
                new_field
            )
        ], project)

    yield calls
项目:django-wechat-api    作者:crazy-canux    | 项目源码 | 文件源码
def reduce_alter_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 [
                other,
                migrations.AlterField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                ),
            ]