Is there an approved method to completely remove an app? - django

I have an app which was misconceived, and I would now like to delete it completely. I cannot find anything in the Django documentation about the right "blessed" way to do this.
I tried commenting out its models and running makemigrations but this threw errors because its views.py could not import them. If I removed its views from urls.py and tried, then makemigrations did not recognise that anything had changed.
Is it as simple as removing it from installed_apps in settings.py, removing its code, and manually deleting its one small model table from the DB? Or will the fact that it had an admin.py mean that there are dangling references left somewhere?
I'm using Django 2.2 if it makes any differerence

From experience:
Remove any foreign key relations to this delete_app's models
Make the migrations for those removals and apply
Delete all the references to delete_app from other apps
Delete all the code in delete_app except migrations
Make the migrations for the deleted models and apply
After this I'm still left with delete_app's migrations and its entry in INSTALLED_APPS because other migrations from other apps still refer some dependency on delete_app's migrations. Haven't had time to check how I can remove it completely, or if there is a recommended way of doing this, but hope this helps.

Related

Django 2.1.7 Clean Way to delete a single App?

i ask myself if there is a clean way/ procedure to ask Django to delete a single app. There are Topics with the same Question but they referencing to older Versions or i missed it.
I am newbie i donĀ“t want to mess around with the django db by simply delete the Setting.py App registration. Because I saw that in the Django DB, between all the model entries, there are also data records of migrations, sessions, data entries or how i would call it, django magic.
I am using Django 2.1.7 within VirtualEnv.
So Far my procedure.
Delete the 'my_old_app' App-Registration in settings.py / INSTALLED_APPS
Change all entries in views.py, urls.py that have a connection to my_old_app
Deleting the model data of my_old_app. (simply Migrate?)
Thanks, for every Information.
You could follow these steps to delete an app in a good way.
delete unnecessary model in `models.py` and `migrate`
delete your app name from `settings.py` in INSTALLED_APPS
delete all folder `__pycache__ `
delete all import link in `views.py`, `admin.py` end etc.
delete all link's in `urls.py` that are unnecessary
delete app's folder
You could make use of --dry-run as mentioned by #Red Cricket in comments

Don't apply existing migrations for a Django model

I need to tell Django not to apply already existing migrations for a model. Is there a way I can achieve it?
Why: I have some customizations on top of django.contrib.auth. With those, Group model is left unused. However, migrations for it are included into the auth app. Unlike User, Group is not swappable.
You can set MIGRATION_MODULES and django will use migrations from setted directory for app
MIGRATION_MODULES = {'django.contrib.auth': 'local_package'}
You can simply edit the migrations files.
So, simply comment out the parts you don't want to be applied.
You can also set your Model to be managed=False , but I'm not sure if that is what you need.

Uninstalling an app breaks old migration

I've stopped using a 3rd party app which I no longer need so I've uninstalled from my virtualenv. However, that causes all the old migrations which reference models from that app from failing when I migrate.
from menu.models import MenuItem
ImportError: No module named menu.models
The only two options I can think of are to either leave the 3rd party app installed just to satisfy the migration or to edit the old migration to remove the reference to the now defunct app.
Neither seem ideal. Any other way I've not thought of?
According to Django Migrations Historical Models docs:
... the base classes of the model are just stored as pointers, so you
must always keep base classes around for as long as there is a
migration that contains a reference to them.
So, you should keep historical apps in your Virtualenv till you remove the migration files that have references to historical models.
The ways to remove migrations files are:
Squashing Migrations, or
Removing migration files and migrations records in the database. CAUTION: Do NOT do that if you are not really sure what are you doing, look at this answers for more info: How to reset migrations in Django 1.7?.

Can I add any column after I created My database?

I created my database using python manage.py syncdb And I tried to add another attribute to my model called created_date My site gives error. And I deleted my db.sqlite3 file Then reorganize my model Then error went. I want to know is this correct
As the comment says migrations are the way to do this. Native migrations were only introduced in Django 1.7 but, since you're using syncdb, I'm guessing you're using an earlier version.
For earlier versions of Django you need a third-party app called South to handle migrations for you. This will then let you change your database after creation fairly painlessly in most cases.

Django manytomany field failing with "relation does not exist" due to model being in separate app. Change order of app creation?

I have a model in an app (we'll call it Report) that is referencing a manytomany on another model in a separate app (Notes). When I run manage.py syncdb I keep getting the error relation does not exist because the database table for the Notes model hasn't been created yet. Is there a way to control the order in which syncdb will create database table to fix this problem? My first guess was the ordering of the apps in installed_apps but Notes is definitely before the Report in installed_apps.
Check for, and resolve, any circular imports. This is when one models.py imports from another which also imports from the original. For example, App Report imports models from Notes, but Notes also imports from Report. The only way to change the order of creation, is to change the order of the INSTALLED_APPS and the order in which they are in the models.py files. As jpic said, you can reference the M2M reference object with quotes and the app name dot model name, like:
class Report(models.Model):
.
.
.
whatevers = ManyToManyField('Notes.whatever_model')
.
.
.