加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何从django项目(及其所有表)中删除应用程序

发布时间:2020-12-16 23:30:15 所属栏目:Python 来源:网络整理
导读:我想从 django项目中删除一个应用程序. 我想删除 应用程序的表格 内容类型 这些内容类型的外键使用 运行manage.py migrate app_to_remove零不起作用: django.db.migrations.migration.IrreversibleError: Operation RunPython function forwards_func at 0x
我想从 django项目中删除一个应用程序.

我想删除

>应用程序的表格
>内容类型
>这些内容类型的外键使用

运行manage.py migrate app_to_remove零不起作用:

django.db.migrations.migration.IrreversibleError: 
Operation <RunPython <function forwards_func at 0x7ff76075d668>> in
            fooapp.0007_add_bar is not reversible

我想有几次迁移是不可逆的……

解决方法

第一:删除代码中的引用

>从settings.INSTALLED_APPS中删除app_to_remove
>删除urls.py或其他地方的其他引用

第二:清理数据库

为您的django项目创建一个空迁移:

manage.py makemigrations your_django_project --empty

编辑文件.这是一个模板:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations,models


class Migration(migrations.Migration):

    dependencies = [
        ('your_django_project','0001_initial'),]

    operations = [
        migrations.RunSQL('''
        drop if exists table app_to_remove_table1;
        drop if exists table app_to_remove_table2;
        ....
        delete from auth_permission where content_type_id in (select id from django_content_type where app_label = '{app_label}');
        delete from django_admin_log where content_type_id in (select id from django_content_type where app_label = '{app_label}');
        delete from reversion_version where content_type_id in (select id from django_content_type where app_label = '{app_label}');
        delete from django_content_type where app_label = '{app_label}';
        delete from django_migrations where app='{app_label}';
        '''.format(app_label='app_to_remove'))
    ]

运行迁移,运行测试.

关于“drop if exists”:你有两种情况:

>生产系统:您想放弃表格.>新的开发系统:这些系统从来没有这个应用程序,他们没有这个表:-)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读