Skip to content

Instantly share code, notes, and snippets.

@akkuman
Created June 28, 2023 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkuman/d9e568d0d46f7e05b9b9e020ada15585 to your computer and use it in GitHub Desktop.
Save akkuman/d9e568d0d46f7e05b9b9e020ada15585 to your computer and use it in GitHub Desktop.
django迁移中进行数据库操作
from django.db import migrations, models
import django.db.models.manager
def delete_all(apps, schema_editor):
'''删除所有的老旧poc'''
Poc = apps.get_model("vuln_scan", "Poc")
# 此处使用 hard_delete_objects 的原因参见 https://stackoverflow.com/a/69767087/16654916
# 因 safedelete 的 Manager 均未设置 use_in_migrations = True,故不可用
Poc.hard_delete_objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
("vuln_scan", "0013_alter_poc_security"),
]
operations = [
# 这一行是必须的,因为检查之前的迁移后,并未发现 poc 表上 hard_delete_objects 的迁移变更
# ref: https://stackoverflow.com/a/71244427
migrations.AlterModelManagers(
name='poc',
managers=[
('hard_delete_objects', django.db.models.manager.Manager()),
],
),
migrations.RunPython(delete_all, migrations.RunPython.noop),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment