Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make two django projects share the same database

Tags:

python

django

I need to make two separate Django projects share the same database. In project_1 I have models creating objects that I need to use in project_2 (mostly images).

The tree structure of project_1_2 is:

project_1/
    manage.py
    settings.py
    project_1_app1/
      ...
    ...

project_2/
    manage.py
    settings.py
    project_2_app1/
      ...
    ...

Which is the best approach?

EDIT: I'm using sqlite3 in my development environment.

I'd like to keep my two django projects as stand-alone projects (so that both can be upgraded safely from their respective repositories).

# in project_1/settings.py
import os

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
..

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_ROOT, 'development.db'),
    },
}
...
# in project_2/settings.py
import os

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
..

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

In this way, each project has its own development.db (the one that I need to be shared):

project_1/development.db 
project_2/development.db

but I guess I need to do something more to make it shared (and unique). The best for me would be to keep the development.db at project_1/ path and thus set the project_2/settings.py DATABASES to point to project_1/development.db.

like image 509
user123892 Avatar asked Sep 29 '16 15:09

user123892


People also ask

Can you have two Django apps on one database?

Multiple Apps can point to one database. What happens is that django creates different tables in the database. You received this message because you are subscribed to the Google Groups "Django users" group.

Can you use multiple databases within the same Django projects?

Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.


2 Answers

You can simply define the same database in DATABASES in your settings.py. So, if your database is PostgreSQL, you could do something like this:

# in project_1/settings.py

DATABASES = {
    'default': {
        'NAME': 'common_db',
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'project_1_user',
        'PASSWORD': 'strong_password_1'
    },
}

# in project_2/settings.py

DATABASES = {
    'default': {
        'NAME': 'common_db',
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'project_2_user',
        'PASSWORD': 'strong_password_2'
    },
}

Note that both database users (project_1_user and project_2_user) should have the appropriate privileges on the database you wish to use. Or you could instead use the same user for both projects.

If you want to have more than just one database per project, you should take a look at the docs for multiple databases.

On another matter, since you share data, I guess you share functionalities as well. So for example, if project_1_app1 and project_2_app1 do same (or similar) things, it seems they could instead be a single reusable app.

Edit

Since you use sqlite3, you should ensure the path you use is the same. So, assuming that project_1 and project_2 are siblings, like so:

projects
  project_1
    settings.py
    ...
  project_2
    settings.py
    ...

you should try this:

# project_1/settings.py

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


# project_2/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(
            os.path.dirname(os.path.dirname(PROJECT_ROOT)),
            'project_1',
            'development.db'
        ),
    },
}

This would give the structure you ask for. Note however that the projects are not both "standalone". project_2 is clearly dependent on project_1's database.

In any case, perhaps, you should also take a look at the os.path module for more info.

like image 150
alxs Avatar answered Sep 22 '22 21:09

alxs


You just need to declare in your model in class meta the attribute db_table with a name diferent the name of app + model (Which are automatically generated by Django) the twice projects need the same models. before the run makemigrations and migrate.

class MyModel(models.Model):
    class Meta:
        db_table = 'MyModel'
like image 45
Gabriel Ferrante Avatar answered Sep 25 '22 21:09

Gabriel Ferrante