如何使两个django项目共享相同的数据库 [英] How to make two django projects share the same database

查看:822
本文介绍了如何使两个django项目共享相同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使两个单独的Django项目共享相同的数据库。在 project_1 我有模型创建我需要在 project_2 (主要是图像)中使用的对象。

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).

project_1_2 的树结构是:

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

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

哪种是最好的方法?

编辑:我正在使用sqlite3开发环境。

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

我想将我的两个django项目作为独立项目(这两个可以从各自的存储库安全升级)。

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'),
    },
}
...

以这种方式,每个项目都有自己的 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

但我想我需要做更多的事情来做mak它是共享(和唯一)。
对于我来说,最好的办法就是在 project_1 / path 之间保留 development.db ,从而设置project_2 / settings .py DATABASES 指向 project_1 / 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.

推荐答案

您可以在settings.py中的 DATABASES 中简单定义相同的数据库。所以,如果你的数据库是PostgreSQL,你可以这样做:

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'
    },
}

请注意,数据库用户( project_1_user project_2_user )应该具有适当的权限您要使用的数据库。或者你可以改用这两个项目使用相同的用户。

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.

另一件事,既然你共享数据,我想你也分享功能。所以例如,如果 project_1_app1 project_2_app1 做同样的(或类似的)事情,似乎可以改为单可重复使用的应用

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.

编辑

由于您使用sqlite3,您应该确保使用的路径相同。因此,假设 project_1 project_2 是兄弟姐妹,如下所示:

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
    ...

你应该尝试这样:

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

这将给你要的结构。但是请注意,这些项目不是独立的。 project_2 显然依赖于 project_1 的数据库。

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.

无论如何,也许你也应该看看 os.path 模块以获取更多信息。

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

这篇关于如何使两个django项目共享相同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆