1
votes

I previously had SQLite default database with Django and was using it for user authentication. I am building a CMS, hence I need to store images. So I added a PostgreSQL database in settings.py and created new models for images in models.py. How do I migrate these new changes to the newly created database? If I do

python manage.py migrate

the newly created models will be stored in SQLite database only right?

1

1 Answers

1
votes

You have to specify your databases in settings.py files like this :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
    'postgresql': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Then, in your project folder, you can create a file named : routersGlobal.py

from django.conf import settings

class GlobalRouter(object):
    """
A router to control all database operations on models in the
auth application.
"""

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)

        if model._meta.app_label in app_list:
            return 'default' #According to database name sqlite3
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)
        if model._meta.app_label in app_list:
            return 'default'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)
        if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)

        if app_label in app_list:
            return db == 'default'
        return None

And you can create an other file which named : routersLocal.py file :

from django.conf import settings

class LocalRouter(object):
    """
A router to control all database operations on models in the
auth application.
"""

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth.
        """
        app_list = ('YourNewApp',)

        if model._meta.app_label in app_list:
            return 'postgresql'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth.
        """
        app_list = ('YourNewApp',)
        if model._meta.app_label in app_list:
            return 'postgresql'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        app_list = ('YourNewApp',)
        if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        app_list = ('YourNewApp',)

        if app_label in app_list:
            return db == 'postgresql'
        return None

And finally in settings.py file you have to specified redirections :

DATABASE_ROUTERS = ['YourProjectName.routersLocal.LocalRouter', 'YourProjectName.routersGlobal.GlobalRouter']

Then, it should work and apply dissociate migrations