如何从Django项目(及其所有表)中删除应用 [英] How to remove an app from a django projects (and all its tables)

查看:114
本文介绍了如何从Django项目(及其所有表)中删除应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Django项目中删除应用。

I want to remove an app from a django project.

我想删除


  • 应用程序的表

  • 内容类型

  • 这些内容类型的外键用法

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

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

我想有些迁移是不可逆的...

I guess there are several migrations which are not reversible ...

推荐答案

首先:删除代码中的引用




  • 设置中删除 app_to_remove 。INSTALLED_APPS

  • 删除 urls.py 或其他位置的其他引用

  • First: Remove references in the code

    • remove app_to_remove from settings.INSTALLED_APPS
    • remove other references in urls.py or other places
    • 为Django项目创建一个空迁移:

      Create an empty migration for your django-project:

      manage.py makemigrations your_django_project --empty
      

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

      Edit the file. Here is a template:

      # -*- 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'))
          ]
      

      运行迁移,运行测试。

      关于如果存在则删除:您有两种情况:

      About "drop if exists": You have two cases:


      1. 生产系统:您要删除表。

      2. 新开发系统:这些系统从未安装过该应用,并且也没有该表格:- )

      这篇关于如何从Django项目(及其所有表)中删除应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆