How do you selectively migrate Django models? - django

Given any standard Django models.py file, how can you tell Django to include / exclude certain models conditionally (e.g. based on variables in the settings.py module) when running python manage.py makemigrations [app_name]?

When running python manage.py makemigrations [app_name] you cannot exclude certain models. You can write migrations manually, read the documentation: Writing database migrations.
Or run python manage.py makemigrations [app_name] and then edit the generated migrations file to delete operations that construct models you want to exclude.

Related

Django sperate migrations for seprate tenants with seperate apps,in same database

I am trying to make two tenants with separate apps and models, the migrate_schemas make migrations for all the apps so need to separate the migrations is it possible
Use python manage.py makemigrations <app_name> and python manage.py migrate <app_name> for separate app migrations.

Is it possible to make migrations from db not from model?

Suppose, we have a db's backup and a django program. The program do not have any migrations. First we restore db, that has created table and data. Now we want to make migrations from available db. Is it possible or not?
Yes, Django has the inspectdb method, which is described here.
But if the Django app already has the models defined that correspond to the backed up database, then you can just run makemigrations (follow #Shafikur's instructions).
Just go to your corresponding database terminals and delete all the records from you django_migrations table with
delete from django_migrations;
Go to terminal and run remove all files in migrations folder with
rm -rf <app>/migrations/
Reset all the migrations of the Django's built-in apps like admin with the command
python manage.py migrate --fake
Create initial migrations for each and every app:
python manage.py makemigrations <app>
To create initial fake migrations just run
python manage.py migrate --fake-initial

Django makemigrations app order

I'm using Django 1.8.4. As my project is still under construction, I frequently remove all migration scripts, and rerun makemigrations to generate the initial migration scripts.
I found makemigrations would generate two migration scripts for one of my apps while other apps just have 0001_initial.py. It would be something like:
- 0001_initial.py
- 0002_auto_20150919_1645.py
I checked the content of 0002_auto_20150919_1645.py, it was adding foreign field from the other app's model.
I guess it might be related to the order of creating migrations for apps. So I delete these two migration scripts of this app and then run makemigrations again. Now I have only one migration script for this app.
My questions is:
Is there any way I can control the order makemigrations create migrations for apps?
For example, I have two apps, app1 and app2, and app1 depends on app2. Is it possible makemigrations create migration for app2 first, and then app1?
You can manually run migrations for an individual app.
./manage.py makemigrations app2
./manage.py makemigrations app1
./manage.py makemigrations # migrate the rest of your apps
You could also squash your existing migrations.

Django Migrations command workflow

There are three migration commands in Django:
python manage.py makemigrations
python manage.py migrate
python manage.py syncdb
In what order do these commands should be executed in terms of workflow of a basic project?
I am using Version: 1.8
syncdb is deprecated and does the same as migrate.
Whenever you make a change to your models, and when you first create them, each time you'd want to first run makemigrations to create the migration files, then migrate to apply them to your database.

django models Changing the model details

If I change certain attributes of a model, say add a column or change the type (or) size constraint etc, then I do
python manage.py sql myapp
python manage.py syncdb
The change doesnt get effected in the app. The change is effected only if i delete the table from my sql and give the commands again.
Is this the only way or are there any other easy ways to do it?
Yes. Get django-south and use it to migrate changes.
It's one of the most used django projects in the world and does exactly what you need.
Instead of running
python manage.py sycndb
You run
python manage.py schemamigration appname --initial
or
python manage.py schemamigration appname --auto
Or any of the other command combos. Check them out.