How to make two django projects share the same database
<p>I need to make two separate Django projects share the same database. In <code>project_1</code> I have models creating objects that I need to use in <code>project_2</code> (mostly images).</p>

<p>The tree structure of <code>project_1_2</code> is:</p>

<pre><code>project_1/
    manage.py
    settings.py
    project_1_app1/
      ...
    ...

project_2/
    manage.py
    settings.py
    project_2_app1/
      ...
    ...
</code></pre>

<p>Which is the best approach?</p>

<p><strong>EDIT</strong>: I'm using sqlite3 in my development environment.</p>

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

<pre><code># 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'),
    },
}
...
</code></pre>

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

<pre><code>project_1/development.db 
project_2/development.db
</code></pre>

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

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

<pre><code># 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'
    },
}
</code></pre>

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

<p>If you want to have more than just one database per project, you should take a look at the <a href="https://docs.djangoproject.com/en/1.10/topics/db/multi-db/" rel="nofollow">docs for multiple databases</a>.</p>

<p>On another matter, since you share data, I guess you share functionalities as well. So for example, if <code>project_1_app1</code> and <code>project_2_app1</code> do same (or similar) things, it seems they could instead be a single <a href="https://docs.djangoproject.com/en/1.10/intro/reusable-apps/" rel="nofollow">reusable app</a>.</p>

<p><strong>Edit</strong></p>

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

<pre><code>projects
  project_1
    settings.py
    ...
  project_2
    settings.py
    ...
</code></pre>

<p>you should try this:</p>

<pre><code># 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'
        ),
    },
}
</code></pre>

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

<p>In any case, perhaps, you should also take a look at the <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow">os.path</a> module for more info.</p>

python django