def remove_delete_protection(*models):
"""
Temporarily removes delete protection on any number of models
Args:
*models: One or more models whose tables will have delete protection temporarily removed
"""
table_names = [model._meta.db_table for model in models]
with connection.cursor() as cursor:
for table_name in table_names:
cursor.execute("DROP RULE delete_protect ON {}".format(table_name))
try:
yield
finally:
for table_name in reversed(table_names):
cursor.execute("CREATE RULE delete_protect AS ON DELETE TO {} DO INSTEAD NOTHING".format(table_name))
评论列表
文章目录