11

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?

2
  • If you want to exclude whole app's models then it can be done. Jul 25, 2017 at 15:46
  • @ArpitSolanki that's exactly what I want to do! Do you know how I can do that? Jul 25, 2017 at 15:57

3 Answers 3

8

remove the app from installed apps in your settings.

4
  • 3
    and you have to add app_label = 'the_app' in class Meta of each model of the app
    – epineda
    Jul 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.
    – Alfa Bravo
    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.
    – Alfa Bravo
    Aug 26, 2019 at 12:33
7

Removing the __init__.py file from within the apps migrations directory should work.

4
  • 3
    This 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 and migrate 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
    Hmm, not sure about dynamic models, but maybe you could "touch" the __init__.py file at runtime, using os or pathlib libraries, after making the models? link
    – Ben Petty
    Jul 25, 2017 at 22:37
  • 2
    This 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
0

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

Search duckduckgo for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.