def create_drop_model(field, filters: List[str]):
"""Creates and drops a model with the specified field.
Arguments:
field:
The field to include on the
model to create and drop.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model({'title': field})
with filtered_schema_editor(*filters) as (schema_editor, calls):
execute_migration(schema_editor, [
migrations.CreateModel(
model.__name__,
fields=[
('title', field.clone())
]
),
migrations.DeleteModel(
model.__name__,
)
])
yield calls
python类DeleteModel()的实例源码
def test_delete_model(self):
project_state = self.set_up_test_model()
operation = migrations.DeleteModel("Pony")
new_state = project_state.clone()
operation.state_forwards('tests', new_state)
self.assertTableExists('tests_pony')
with connection.schema_editor() as editor:
operation.database_forwards('tests', editor, project_state, new_state)
self.assertTableNotExists('tests_pony')
with connection.schema_editor() as editor:
operation.database_backwards('tests', editor, new_state, project_state)
self.assertTableExists('tests_pony')
def reduce_model_create_delete(self, operation, other, in_between):
"""
Folds a CreateModel and a DeleteModel into nothing.
"""
if (operation.name_lower == other.name_lower and
not operation.options.get("proxy", False)):
return []
def reduce_model_alter_delete(self, operation, other, in_between):
"""
Folds an AlterModelSomething and a DeleteModel into just delete.
"""
if operation.name_lower == other.name_lower:
return [other]