Is there a way to exclude models of an app from Django migrations? I know changing the model meta options with managed = False
is an option but that's a lot of models to edit every time. Is there a way to specify an app whose models I don't want to migrate?
-
If you want to exclude whole app's models then it can be done.– Arpit SolankiJul 25, 2017 at 15:46
-
@ArpitSolanki that's exactly what I want to do! Do you know how I can do that?– cookiedoughJul 25, 2017 at 15:57
Add a comment
|
3 Answers
remove the app from installed apps in your settings.
-
3and you have to add
app_label = 'the_app'
inclass Meta
of each model of the app– epinedaJul 31, 2019 at 3:43 -
@epineda Thanks for the comment, this worked for me. The approved answer recreated the `__init__.py file everytime for me. Aug 9, 2019 at 9:11
-
@AlfaBravo Are you using an IDE? or just a slim text editor? That's probably your IDE adding them back if you are running the command through the IDE. Aug 26, 2019 at 12:18
-
@IanKirkpatrick I am using vs-code, but thanks for the heads up. When I have this problem again I would try and see if there is a difference in the two use cases. Aug 26, 2019 at 12:33
Removing the __init__.py
file from within the apps migrations directory should work.
-
3This answer is the right answer to specifically the question I asked. In my case however, I am using dynamic models. When I generate them at runtime, I run
makemigrations
andmigrate
to add them to the models of the app. So this in my case will not work. Do you have any ideas for working with dynamic models? Jul 25, 2017 at 21:07 -
1
-
2This is a hack way of doing this. Yes it works, but that's because it makes the migrations directory not a package. The migration files are no longer importable... that's why they don't work... they aren't even considered code. The proper way to do this is to tell Django not to include them... not to effectively wipe them from existance. Aug 26, 2019 at 12:15
-
Although I suppose if you only want to exclude it 1 time and then continue including it always, this is a fine way of doing this as long as you add the init.py back right away so that Django can see the files again. Aug 26, 2019 at 12:16
You can do this if you use database routers with
def allow_migrate(self, db, app_label, model_name=None, **hints):
return False
Example.
class FilemakerRouter:
"""
A router to control all database operations on models in the
filemaker application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read filemaker models go to filemaker.
"""
if model._meta.app_label == 'filemaker':
return 'filemaker'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write filemaker models go to filemaker.
"""
if model._meta.app_label == 'filemaker':
return 'filemaker'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the filemaker app is involved.
"""
if obj1._meta.app_label == 'filemaker' or \
obj2._meta.app_label == 'filemaker':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
return False