Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - How to make two django projects share the same database

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...