Uninstalling an app breaks old migration - django

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

Related

Django Apps with shared model migrate to multi schemas Postgres

In a Django project, I have a few apps and one model-class will be created in one app's models.py and imported to another apps' models.py. then I run migrate for each app per one schema in Postgres, there is no error reported.
But the issue is the shared model will be migrated to all the schemas because (I assume this is the reason) I imported this model in each app's models.py.
Anyone knows How to avoid or solve this problem. Because I would like keep the model only stay in his original schema.
Or can I delete this model directly from each schema it appears?...without any side effects.
Please help, Thanks!
I found a better answer: Installing PostgreSQL Extension to all schemas.
should see this before.
Accidentally found the solution, just use 'public' schema for the app where models supposed to import to other apps.
Just for someone who experience the same situation.

Is there an approved method to completely remove an app?

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.

reusable app (package) in Django - how to add extra models

I am writing a small package that extends the django app that is used by many of my colleagues locally. So right now they can simply add it via pip, and then they add this extension in INSTALLED_APPS in settings.py.
But the problem is that I can't add the new models to this extension (or at least I don't figure out yet how to do it correctly) because then the guys who would like to use my extension, have to sync their database or make migrations so their database contains the models needed for extension.
Is it possible (and right thing to do) to add new models to the current django project 'silently' as soon as the user adds the app to INSTALLED_APPS?

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 not creating tables for an installed app

My django site was functioning before I installed Lion and had to reinstall everything related to development. Since then, I have deleted and recreated my database, but one of my two installed apps is being ignored in syncdb. Those tables are not present in my database.
This post suggested there might be an import error. I can import the app in question using manage.py shell, so I don't think that's it.
Both apps are definitely installed (verified by debug toolbar). Any other suggestions? I'm relatively new to Django, having been mostly an iOS developer for the past couple of years.
https://docs.djangoproject.com/en/dev/ref/models/options/#app-label
If a model exists outside of the standard models.py (for instance, if the app’s models are in submodules of myapp.models), the model must define which app it is part of.
What it doesn't mention is that they also have to be imported somewhere during the model registration phase.