如何完全卸载 Django 应用程序?

发布于 2024-09-11 07:23:31 字数 36 浏览 2 评论 0原文

完全卸载 Django 应用程序并删除数据库的过程是什么?

What is the procedure for completely uninstalling a Django app, complete with database removal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

两人的回忆 2024-09-18 07:23:31
  1. 姜戈

    1.7 有一个方便的管理命令,它将为您提供删除应用程序的所有表所需的 SQL。请参阅 sqlclear 文档了解更多信息。基本上,运行 ./manage.py sqlclear my_app_name 可以让您获得应该执行的 SQL 语句,以消除数据库中应用程序的所有痕迹。您仍然需要将这些语句复制并粘贴(或通过管道传输)到 SQL 客户端中。对于 Django 1.7 及更高版本,请使用 ./manage.py migrate my_app_name Zero (请参阅 migrate docs),它会自动运行数据库清理。

  2. 要从项目中删除该应用,您只需从项目的 settings.py 中的 INSTALLED_APPS 中删除它即可。 Django 将不再加载该应用程序。

  3. 如果您不再希望应用程序的文件徘徊,请从项目目录或 PYTHONPATH 上它所在的其他位置删除该应用程序目录。

    如果

  4. (可选)如果应用程序在某处存储了媒体文件、缓存文件或其他临时文件,您可能也想删除它们。还要警惕应用程序中可能残留的挥之不去的会话数据。

  5. (可选)我还会删除任何过时的内容类型。

就像这样。

from django.contrib.contenttypes.models import ContentType
for c in ContentType.objects.all():
    if not c.model_class():
        print "deleting %s"%c # print(f"deleting {c}") # for Python 3.6+
        c.delete()
  1. Django < 1.7 has a handy management command that will give you the necessary SQL to drop all the tables for an app. See the sqlclear docs for more information. Basically, running ./manage.py sqlclear my_app_name gets you get the SQL statements that should be executed to get rid of all traces of the app in your DB. You still need to copy and paste (or pipe) those statements into your SQL client. For Django 1.7 and up, use ./manage.py migrate my_app_name zero (see the migrate docs), which runs the database cleaning automatically.

  2. To remove the app from your project, all you need to do is remove it from INSTALLED_APPS in your project's settings.py. Django will no longer load the app.

  3. If you no longer want the app's files hanging around, delete the app directory from your project directory or other location on your PYTHONPATH where it resides.

  4. (optional) If the app stored media files, cache files, or other temporary files somewhere, you may want to delete those as well. Also be wary of lingering session data that might be leftover from the app.

  5. (optional) I would also remove any stale content types.

Like so.

from django.contrib.contenttypes.models import ContentType
for c in ContentType.objects.all():
    if not c.model_class():
        print "deleting %s"%c # print(f"deleting {c}") # for Python 3.6+
        c.delete()
叹沉浮 2024-09-18 07:23:31

在我的上下文中,项目存在多次:我有一个开发系统,一些团队成员有一个开发系统,有一个面向客户的登台系统和一个生产系统。这意味着我不想手动执行sql命令。我希望它能够自动化。

目标:删除应用程序和所有数据库表。

第 1 步:清空应用程序,但保持安装状态,

删除应用程序中的所有文件(文件夹“migrations”除外)

执行此命令:

python manage.py makemigrations -n drop_all_tables my_app_to_remove

目录现在如下所示:

my_app_to_remove/
my_app_to_remove/__init__.py
my_app_to_remove/migrations
my_app_to_remove/migrations/0001_initial.py
my_app_to_remove/migrations/....
my_app_to_remove/migrations/0030_drop_all_tables.py
my_app_to_remove/migrations/__init__.py

my_app_to_remove 保留在文件“settings.txt”中。 py”。

步骤 2:部署更改

更新所有项目。告诉团队成员更新他们的项目并运行迁移。

步骤 3:从 settings.py 中删除“my_app_to_remove”

现在从 settings.py 中删除“my_app_to_remove”并再次部署。

In my context the projects exists several times: I have a development system, some team mates have a development system, there is a staging system for the customer and a production system. This means I don't want to execute sql commands by hand. I want it to be automated.

Goal: Remove the app and all database tables.

Step 1: empty the app, but leave it installed

remove all files from the app, except the folder "migrations"

Execute this command:

python manage.py makemigrations -n drop_all_tables my_app_to_remove

The directory looks now like this:

my_app_to_remove/
my_app_to_remove/__init__.py
my_app_to_remove/migrations
my_app_to_remove/migrations/0001_initial.py
my_app_to_remove/migrations/....
my_app_to_remove/migrations/0030_drop_all_tables.py
my_app_to_remove/migrations/__init__.py

Leave my_app_to_remove in the file "settings.py".

Step 2: Deploy the changes

Update all projects. Tell the team mates to update their project and to run the migrations.

Step 3: remove "my_app_to_remove" from settings.py

Now remove "my_app_to_remove" from settings.py and deploy again.

内心荒芜 2024-09-18 07:23:31
  1. 注释掉 INSTALLED_APPS 中不必要的应用程序行中的 settings.py
  2. 删除项目中的所有文件夹 __pycache__migrate
  3. 删除不必要的models.py 中的 model
  4. 删除 views.py 中的所有 import 链接,admin.py end 等
  5. 删除所有 数据库中不必要的应用程序的 urls.py 中的链接
  6. 删除与该应用程序关联的不必要的表(我使用帮助程序“Valentina Studio”执行此操作)
  7. 在命令行中删除应用程序的文件夹
  8. 执行此操作:python manage.py migratepython manage.pysyncdb
  1. comment out on settings.py in INSTALLED_APPS unnecessary app's line
  2. delete all folder __pycache__ and migrate at your project
  3. delete unnecessary model in models.py
  4. delete all import link in views.py, admin.py end etc.
  5. delete all link's in urls.py on your unnecessary app's
  6. in database delete unnecessary tables wich associated with the app (I do it with help program "Valentina Studio")
  7. delete app's folder
  8. in command line do it: python manage.py migrate and python manage.py syncdb
遥远的她 2024-09-18 07:23:31

第一步,阻止对应用程序模型的任何使用并进行部署。这对于滚动部署至关重要。检查下面的注释。

那么你有两个选择。

选项 1

  1. (手动)运行 python manage.py migrate;零。这将恢复应用程序的所有迁移并清理 django_migrations
  2. 删除应用程序(代码、INSTALLED_APPSurls.py 等)
  3. 部署(python manage.py migrate)

选项 2

  1. 删除应用程序中的所有模型
  2. 部署
  3. 删除应用程序 (code, INSTALLED_APPS, urls. py 等)
  4. 部署
  5. (手动)清理 django_migrations

这个答案是在自动滚动部署上下文(例如 Heroku)中思考的。
手动意味着在自动化部署中通常无法完成。部署基本上指的是python manage.py migrate,它通常在自动化部署中完成。

注释

  1. 确保删除从其他应用导入此应用的代码。还有 settings.py 中的任何引用(但将其保留在 INSTALLED_APPS 中,以便我们可以运行迁移)、urls.py 等。
  2. 迁移将根据这些迁移恢复所有迁移。因此,如果您的其他应用程序迁移依赖于这些迁移,请务必小心。
  3. 如果您不仅有架构迁移还有数据迁移,还要注意级联删除。
  4. 接收其他应用程序中定义的信号的信号接收器可能是滚动部署中的问题。

参考

  1. https://docs.djangoproject。 com/en/stable/ref/django-admin/#migrate

As a first step, prevent any usage of the app models and deploy. This is essential for rolling deployment. Check notes below.

Then you have two options.

Option 1

  1. (Manual) Run python manage.py migrate <app_name> zero. This will revert all the migrations for the app and clean the django_migrations table
  2. Remove app (code, INSTALLED_APPS, urls.py, etc.)
  3. Deploy (python manage.py migrate)

Option 2

  1. Remove all models in the app
  2. Deploy
  3. Remove the app (code, INSTALLED_APPS, urls.py, etc.)
  4. Deploy
  5. (Manual) Clean django_migrations table

This answer is thinking in an automated rolling deployment context (e.g. Heroku).
Manual means it normally cannot be done in automated deployment. Deploy basically refers to python manage.py migrate which is normally done in automated deployment.

Notes

  1. Make sure to remove code importing this app from other apps. Also any references in settings.py (but keep it in INSTALLED_APPS so we can run migrations), urls.py, etc.
  2. migrate <app_name> zero will revert all migrations depending on those migrations. So be careful if your other apps migrations depend on these migrations.
  3. Also be aware of cascade delete if you have not just schema migrations but also data migrations.
  4. Signal receivers receiving signals defined in other apps might be an issue in rolling deployment.

References

  1. https://docs.djangoproject.com/en/stable/ref/django-admin/#migrate
白况 2024-09-18 07:23:31

django app 是一组 *.py 文件和一个带有 django-app-name 的目录。因此,您可以简单地删除包含所有 *.py 文件的整个文件夹

要从数据库中“删除”表,您应该使用 DELETE FROM

此外,您必须删除带有 app 的行-name 来自根目录中的setting.py

django app is a "set" of *.py files and a directory with a django-app-name. So you can simply delete the whole folder with all *.py files

To "remove" tables from DB you should use DELETE FROM <app-name_table-names>

Furthermore, you have to delete lines witgh app-name from setting.py in a root directory

悲念泪 2024-09-18 07:23:31

安全删除 Django 应用(作者:Jordan Haines)

完全删除 Django 应用(对于模型),请按照以下步骤操作。假设我们要删除一个名为 note_app 的应用:

  1. 在您的其他应用中搜索任何 note_app 导入。最简单的方法是在项目范围内搜索“from note_app”。您可能希望从搜索中排除 note_app 目录。
  2. 注释掉 note_app.models 中的所有模型。如果您使用 Django admin 注册了 note_app 模型,您可能还需要从 note_app.admin 文件中删除条目。
  3. 尝试运行 Django 应用程序时查找并解决错误。 PS 您可能在第 1 步中错过了一些模型导入。
  4. 请注意,根据您定义 ForeignKey、OneToOneManyToMany 字段的方式,您可能错过了 note_app 模型的一些键在步骤 1 中。重要的是,在继续之前,需要删除从其他应用程序中的模型指向 note_app 模型的任何字段。花一点时间确保没有遗漏这些字段;如果是,则删除剩余的字段并创建将从数据库中删除这些字段的迁移。
  5. 运行makemigrations。这应该创建一个 note_app 迁移,删除您刚刚推荐的所有模型。此迁移还应从其他应用程序的模型中删除引用 note_app 模型的字段。
  6. 运行您的迁移。在删除应用程序目录之前,您必须在所有环境(包括生产环境)中运行迁移。令人惊讶的是,从数据库中删除应用程序模型的迁移将位于您的 note_app 目录中。如果您过早删除应用程序目录,您将在这些迁移有机会清理您的数据库之前删除它们。
  7. 您可能会收到一条通知,表明已删除模型的内容类型已过时。当询问您是否要删除这些内容类型时,回答“是”
  8. Git 提示:提交您的迁移,记下该提交,然后创建一个单独的提交来删除 note_app 目录。当您准备好在临时或生产环境中应用更改时,请签出您记下的提交,运行迁移,然后签出最新提交以删除应用程序目录。您的第一次提交应该在 INSTALLED_APPS 中仍然有 note_app。
  9. 删除包含 note_app 的目录。
  10. INSTALLED_APPS 设置中删除 note_app

就是这样……超级简单:)

Safely Remove a Django app by Jordan Haines

To completely remove a Django app (with models), follow the steps below. Let’s pretend we’re removing an app called note_app:

  1. Search through your other apps for any note_app imports. The easiest way to do this is a project-wide search for “from note_app”. You may want to exclude the note_app directory from your search.
  2. Comment out all models in note_app.models. You may need to also remove entries from note_app.admin file if you registered your note_app models with Django admin.
  3. Look for and resolve errors when trying to run your Django app. P.S. You may have missed some model imports in step 1.
  4. Note that depending on how you define ForeignKey, OneToOne and ManyToMany fields, you may have missed some keys to your note_app models in step 1. It’s important that any fields that point to note_app models from models in other apps need to be deleted before you continue. Take a minute to make sure none of these fields were missed; if they were, then delete those fields that remain and create migrations that will remove the fields from your database.
  5. Run makemigrations. This should create a note_app migration that deletes all of the models you just commended out. This migration should also remove fields referring to note_app models from models in other apps.
  6. Run your migrations. You must run your migrations in all environments (including production) BEFORE you delete the app directory. The migrations that remove your app’s models from the database will be — surprise — in your note_app directory. If you delete the app directory prematurely, you will delete these migrations before they have a chance to clean up your database.
  7. You may get a notice that the content types for your deleted models are stale. When asked whether or not you want to delete these content types, reply “yes”
  8. Git Tip: Commit your migrations, take note of the commit, and then create a separate commit that deletes the note_app directory. When you are ready to apply your changes in a staging or production environment, checkout the commit you noted, run the migration, and then checkout the latest commit to delete the app directory. Your first commit should still have note_app in INSTALLED_APPS.
  9. Delete the directory that contains the note_app.
  10. Remove note_app from your INSTALLED_APPS setting.

And that’s really about it…super easy :)

彡翼 2024-09-18 07:23:31

以下 3 个步骤可以从 Django 项目中完全删除应用程序:

  1. 使用 通过指定它,如下所示。 *下面的命令从数据库中删除应用程序的所有表:
python manage.py migrate <app_name> zero
  • ;:带来...应用程序名称。使用名称零一路迁移回来,即恢复应用程序的所有已应用的迁移。

*注意,您无法通过如下所示指定zero多个应用来迁移它们,那么会出现错误:

python manage.py migrate <app_name> <app_name> zero
  1. Remove the app from INSTALLED_APPS in settings.py如下所示:
# "settings.py"

INSTALLED_APPS = [
    # "my_app",
]
  1. 从Django项目中删除app文件夹。

These 3 steps below can completely remove an app from a Django project:

  1. Migrate an app with zero by specifying it as shown below. *This command below drops all the tables of an app from database:
python manage.py migrate <app_name> zero
  • <app_label> <migrationname>: Brings the ... app name. Use the name zero to migrate all the way back i.e. to revert all applied migrations for an app.

*Be careful, you cannot migrate multiple apps with zero by specifying them as shown below, then there is an error:

python manage.py migrate <app_name> <app_name> zero
  1. Remove the app from INSTALLED_APPS in settings.py as shown below:
# "settings.py"

INSTALLED_APPS = [
    # "my_app",
]
  1. Delete the app folder from a Django project.
鸠书 2024-09-18 07:23:31

之前的答案大多是以下内容的变体: https://patrick.cloke.us/posts/2020/01/23/cleanly-removing-a-django-app-with-models/

Django 现在记录了删除应用程序的“批准”方法:
https://docs.djangoproject.com/en/dev/howto/delete -app/

与下面的脚本相结合,该解决方案...

  1. 可以编写脚本以在部署期间运行;
  2. 从数据库和代码库中完全删除应用程序的所有痕迹;
  3. 使用 Django API,而不是使用原始 SQL 进行清理。

此过程分 2 个阶段完成。

第 1 阶段:删除应用程序模型、迁移和部署...

  • 遵循上面的“官方”django 程序。
  • 注意:在首次部署之前,您可以删除所有应用程序代码
    除了模型(空)、迁移和应用程序(部署所需)之外

第 2 阶段:在将来的某个日期从代码库中删除数据库和应用程序包中的所有跟踪...

  • 运行下面的脚本以删除应用程序模型的所有跟踪)和来自数据库的内容类型。
  • 注意:可能需要对迁移进行一些清理才能使它们全部恢复为零。

第 2 步的完整管理命令:
该命令

的核心内容:

from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from django.contrib.admin.models import LogEntry
from django.core.management import call_command

app_label = 'APP_LABEL_TO_DELETE'  # PARAMETER: the app to be removed

content_types = ContentType.objects.filter(app_label=app_label).all()
permissions = Permission.objects.filter(content_type_id__in=content_types)
log_entries = LogEntry.objects.filter(content_type_id__in=content_types)

# reverse migrations to remove model DB tables and migrations entries
call_command('migrate', app_label, 'zero')

# delete the content type related data
log_entries.delete()
permissions.delete()
content_types.delete()

Prior answers are mostly variations on: https://patrick.cloke.us/posts/2020/01/23/cleanly-removing-a-django-app-with-models/

Django now documents a "sanctioned" method for deleting an app:
https://docs.djangoproject.com/en/dev/howto/delete-app/

Combined with script below, this solution...

  1. can be scripted to run during deployment;
  2. completely removes all traces of the app from DB and code base;
  3. uses Django API rather than dropping to raw SQL for cleanup.

This process is completed in 2 phases.

Phase 1: delete app models, migrate, and deploy...

  • follow "official" django procedure above.
  • Note: prior to first deployment, you can remove all app code
    except models (empty), migrations, and apps (needed for deployment)

Phase 2: remove all traces from DB and app package from code base at some future date...

  • run the script below to remove all traces of the app model(s) and content type(s) from DB.
  • Note: a little cleanup of migrations may be required to get them to all reverse to zero.

A complete management command for Step 2:
https://gist.github.com/powderflask/f0ae8b9ae6c7bb471f591de5175204cd

The heart of that command:

from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from django.contrib.admin.models import LogEntry
from django.core.management import call_command

app_label = 'APP_LABEL_TO_DELETE'  # PARAMETER: the app to be removed

content_types = ContentType.objects.filter(app_label=app_label).all()
permissions = Permission.objects.filter(content_type_id__in=content_types)
log_entries = LogEntry.objects.filter(content_type_id__in=content_types)

# reverse migrations to remove model DB tables and migrations entries
call_command('migrate', app_label, 'zero')

# delete the content type related data
log_entries.delete()
permissions.delete()
content_types.delete()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文