Exclude an app from Django migrations

Viewed 5434

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?

3 Answers

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

Related