H.G.
H.G.

Reputation: 11

How to integrate two Django projects with different databases?

I have my original Django project which runs on sqlite3 database. I want to integrate another Django project from GitHub which runs on postgre database. I added github link in requirements.txt file & installed it and added the new Django project app in original INSTALLED_APPS section of settings.py file and also updated urls.py file of original project. Now I got stuck with how to combine settings.py of these two projects?

When I run command

python manage.py migrate

then it gives me this error:- AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

Correct settings are already in github downloaded django project but not in original project.

I have read almost all answers on Stack Overflow as well as on official docs. But enable to understand. my apologies if this is a duplicate or stupid question.

Upvotes: 1

Views: 962

Answers (1)

Mattia
Mattia

Reputation: 1139

According to the documentation you need to edit your setting by: 1) add a new file calling ex databases.py

DATABASES = {
'default': {},
'auth_db': {
    'NAME': 'auth_db',
    'ENGINE': 'django.db.backends.mysql',
    'USER': 'mysql_user',
    'PASSWORD': 'swordfish',
},
'primary': {
    'NAME': 'primary',
    'ENGINE': 'django.db.backends.mysql',
    'USER': 'mysql_user',
    'PASSWORD': 'spam',
},
'replica1': {
    'NAME': 'replica1',
    'ENGINE': 'django.db.backends.mysql',
    'USER': 'mysql_user',
    'PASSWORD': 'eggs',
},
'replica2': {
    'NAME': 'replica2',
    'ENGINE': 'django.db.backends.mysql',
    'USER': 'mysql_user',
    'PASSWORD': 'bacon',
},

2) for the authrouter file see the documentation, here an example:

import random

class PrimaryReplicaRouter(object):
    def db_for_read(self, model, **hints):
        """
        Reads go to a randomly-chosen replica.
        """
        return random.choice(['replica1', 'replica2'])

    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        return 'primary'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        db_list = ('primary', 'replica1', 'replica2')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All non-auth models end up in this pool.
        """
        return Trueta.app_label == 'auth':
            return 'auth_db'
        return None

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

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

3) in your settings.py remove the database schema and add this line

DATABASE_ROUTERS = ['yourapp.filename.CLASS']

When you will lunch the command "migrate" add --database=databasename for apply them For more info see https://docs.djangoproject.com/en/1.10/topics/db/multi-db/

Upvotes: 3

Related Questions