23👍
First: Remove references in the code
- remove
app_to_remove
fromsettings.INSTALLED_APPS
- remove other references in
urls.py
or other places
Second: Clean the database
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 table if exists app_to_remove_table1;
drop table if exists 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 django_content_type where app_label = '{app_label}';
delete from django_migrations where app='{app_label}';
'''.format(app_label='app_to_remove'))
]
Run the migration, run tests.
About "drop if exists": You have two cases:
- The production system: You want to drop the tables.
- New development systems: These systems never had this app, and they don’t have this table 🙂
2👍
This is what the official documentation suggests for the latest version as of now, which is 4.2:
- Remove all references to the app (imports, foreign keys etc.).
- Remove all models from the corresponding
models.py
file.- Create relevant migrations by running
makemigrations
. This step generates a migration that deletes tables for the removed models, and any other required migration for updating relationships connected to those models.- Squash out references to the app in other apps’ migrations.
- Apply migrations locally, runs tests, and verify the correctness of your project.
- Deploy/release your updated Django project.
- Remove the app from
INSTALLED_APPS
.- Finally, remove the app’s directory.
- How to output text from database with line breaks in a django template?
- Use of python super function in django model
- Selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain while executing tests in Django with Selenium
1👍
Note: this guide is successful with Django 3.1.1 and Python 3.8.2
Can you try this solution to clean your database and migrations first
Step 1: Delete your table from the file your_app/models.py but leave the file itself
Step 2: Check your register in the file admin.py if you have
Step 3: create migration:
manage.py makemigrations your_app
Step 4: migrate into database:
manage.py migrate
you can see the result in my example
Step 5: delete all the files in the folder your_app/migrations
Step 6: Remove migrations in the table django_migrations
python manage.py migrate --fake your_app zero
Check migrations:
python manage.py showmigrations
Step 7: Then you can remove the app completely,check reference,INSTALLED_APPS
- Display some free text in between Django Form fields
- What is difference between instance namespace and application namespace in django urls?
- How do I remove the square brackets at the end of a JS variable name during AJAX calls?
- Ajax, CSRF and DELETE
- How to setup SysLogHandler with Django 1.3 logging dictionary configuration
Source:stackexchange.com