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

前端之家收集整理的这篇文章主要介绍了如何从django项目(及其所有表)中删除应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从 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

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

解决方法

@H_301_17@ 第一:删除代码中的引用

>从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”:你有两种情况:

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

原文链接:https://www.f2er.com/python/185965.html

猜你在找的Python相关文章