Django: How to completely uninstall a Django app?

前端 未结 5 490
星月不相逢
星月不相逢 2020-12-12 10:35

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

相关标签:
5条回答
  • 2020-12-12 11:24

    I really love steps from this article - it is include migration support.

    Maybe it need to be adapt in two Updates of code - but looks really secure when you have to work with many deployments (like: test stage, BETA version and production - in my case)

    0 讨论(0)
  • 2020-12-12 11:27

    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

    0 讨论(0)
  • 2020-12-12 11:28
    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()
    
    0 讨论(0)
    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
    0 讨论(0)
  • 2020-12-12 11:33

    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: 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.

    0 讨论(0)
提交回复
热议问题