nidhi
nidhi

Reputation: 211

how to disable migration from one model of an app in django

I have an application which is using 2 database, 1 is default another is custom. So 2 of my model are using default database, else are using custom database. i do not want to migrate custom database model while running 'make migrations' command. please help it out.

Upvotes: 21

Views: 20065

Answers (4)

Danil Dann
Danil Dann

Reputation: 1

How it worked for me:

  1. Set: managed = False
  2. python manage.py makemigrations
  3. python manage.py migrate (he writes the creation of tables in the migration file, we ignore it).
  4. Set: managed = True (repeat step 2 and 3)
  5. Done (now when adding a new column, the table will not be created in migrations)

Ps. (This worked when the tables were not in relations, when there was a relation between the tables, it took one more time to do manipulations with managed = True/False)

Upvotes: 0

Robert Gabriel
Robert Gabriel

Reputation: 1190

It's worth mentioning that although the managed flag is set to False a migration for the model will still be created but no sql creation script. The sql of a migration ca be check using manage.py sqlmigrate appname migration

Upvotes: 19

Dmitry Shilyaev
Dmitry Shilyaev

Reputation: 733

Options.managed Defaults to True, meaning Django will create the appropriate database tables in migrate or as part of migrations and remove them as part of a flush management command. That is, Django manages the database tables’ lifecycles.

If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal. From the docs

class SomeModel(models.Model):
   class Meta:
       managed = False

Upvotes: 5

elim
elim

Reputation: 1514

You can selectively disable migration for one or more Django-Models by setting managed = False in Django Model Meta options.

from django.db import models
class LegacyModel(models.Model):
   class Meta:
       managed = False

Upvotes: 33

Related Questions