Hello, I am trying to prevent models in my api app from migrating into the 
default database but I'm confused about the router. It seems every time I run 
migrate even with the router it continues to migrate.



#apps.py

from django.apps import AppConfig

class ApiConfig(AppConfig):
    name = 'api'
    label = 'api'



#settings.py

DATABASE_ROUTERS = ['myproject.dev_db_router.APIRouter',]


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'default.sqlite3'),
    },
    'mydb': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'mydb.sqlite3'),
    },
}



#dev_db_router.py

class APIRouter:
    """
    A router to control all database operations on models in the
    api application.
    """
    route_app_labels = {'api',}

    def db_for_read(self, model, **hints):
        """
        Attempts to read api models go to mydb.
        """
        if model._meta.app_label in self.route_app_labels:
            return 'mydb'
        return False

    def db_for_write(self, model, **hints):
        """
        Attempts to write api models goes to mydb.
        """
        if model._meta.app_label in self.route_app_labels:
            return 'mydb'
        return False

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the api app only appears in the
        'mydb' database.
        """
        if app_label in self.route_app_labels:
            return db == 'mydb'
        return False





I've tried:


python manage.py migrate --database=default


python manage.py migrate


etc and every time it says:


  Applying api.0001_initial...* OK*




Even though I told it False if it does not meet the case of db == 'mydb'. I can 
specify 'mydb' and it says it works:


python manage.py migrate --database=mosaic


but my concern is it always migrates into default even when I'm trying to tell 
it not to. In the future there will be models I do want to migrate into 
default, but not these in the api app. Based on the documentation I'm doing 
everything correctly.


What am I not understanding?


Thank you.


Best,


JJ



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/76d56bf9-a870-4a18-8689-6ec93bc50319o%40googlegroups.com.

Reply via email to