本文整理汇总了Python中django.db.migrations.AlterModelTable方法的典型用法代码示例。如果您正苦于以下问题:Python migrations.AlterModelTable方法的具体用法?Python migrations.AlterModelTable怎么用?Python migrations.AlterModelTable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.migrations
的用法示例。
在下文中一共展示了migrations.AlterModelTable方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_alter_model_table_noop
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import AlterModelTable [as 别名]
def test_alter_model_table_noop(self):
"""
Tests the AlterModelTable operation if the table name is not changed.
"""
project_state = self.set_up_test_model("test_almota")
# Test the state alteration
operation = migrations.AlterModelTable("Pony", "test_almota_pony")
new_state = project_state.clone()
operation.state_forwards("test_almota", new_state)
self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony")
# Test the database alteration
self.assertTableExists("test_almota_pony")
with connection.schema_editor() as editor:
operation.database_forwards("test_almota", editor, project_state, new_state)
self.assertTableExists("test_almota_pony")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_almota", editor, new_state, project_state)
self.assertTableExists("test_almota_pony")
示例2: _test_create_alter_foo_delete_model
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import AlterModelTable [as 别名]
def _test_create_alter_foo_delete_model(self, alter_foo):
"""
CreateModel, AlterModelTable, AlterUniqueTogether/AlterIndexTogether/
AlterOrderWithRespectTo, and DeleteModel should collapse into nothing.
"""
self.assertOptimizesTo(
[
migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]),
migrations.AlterModelTable("Foo", "woohoo"),
alter_foo,
migrations.DeleteModel("Foo"),
],
[],
)
示例3: test_alter_alter_table_model
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import AlterModelTable [as 别名]
def test_alter_alter_table_model(self):
self._test_alter_alter_model(
migrations.AlterModelTable("Foo", "a"),
migrations.AlterModelTable("Foo", "b"),
)
示例4: test_alter_model_table
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import AlterModelTable [as 别名]
def test_alter_model_table(self):
"""
Tests the AlterModelTable operation.
"""
project_state = self.set_up_test_model("test_almota")
# Test the state alteration
operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
self.assertEqual(operation.describe(), "Rename table for Pony to test_almota_pony_2")
new_state = project_state.clone()
operation.state_forwards("test_almota", new_state)
self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony_2")
# Test the database alteration
self.assertTableExists("test_almota_pony")
self.assertTableNotExists("test_almota_pony_2")
with connection.schema_editor() as editor:
operation.database_forwards("test_almota", editor, project_state, new_state)
self.assertTableNotExists("test_almota_pony")
self.assertTableExists("test_almota_pony_2")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_almota", editor, new_state, project_state)
self.assertTableExists("test_almota_pony")
self.assertTableNotExists("test_almota_pony_2")
# And deconstruction
definition = operation.deconstruct()
self.assertEqual(definition[0], "AlterModelTable")
self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {'name': "Pony", 'table': "test_almota_pony_2"})
示例5: test_alter_model_table_none
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import AlterModelTable [as 别名]
def test_alter_model_table_none(self):
"""
Tests the AlterModelTable operation if the table name is set to None.
"""
operation = migrations.AlterModelTable("Pony", None)
self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
示例6: alter_db_table
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import AlterModelTable [as 别名]
def alter_db_table(field, filters: List[str]):
"""Creates a model with the specified field and then renames the database
table.
Arguments:
field:
The field to include into the
model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model()
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AlterModelTable(model.__name__, "NewTableName")], state
)
yield calls