我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用django.db.migrations.AlterField()。
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')
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')
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']) ) ])
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']) ) ])
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')
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
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, ), ]