简体   繁体   English

如何让两个django项目共享同一个数据库

[英]How to make two django projects share the same database

I need to make two separate Django projects share the same database.我需要让两个独立的 Django 项目共享同一个数据库。 In project_1 I have models creating objects that I need to use in project_2 (mostly images).project_1我有模型创建我需要在project_2使用的对象(主要是图像)。

The tree structure of project_1_2 is: project_1_2的树结构为:

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.编辑:我在我的开发环境中使用 sqlite3。

I'd like to keep my two django projects as stand-alone projects (so that both can be upgraded safely from their respective repositories).我想将我的两个 django 项目保留为独立项目(以便可以从各自的存储库安全地升级)。

# 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):这样,每个项目都有自己的development.db (我需要分享的那个):

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 .对我来说最好的是将development.db保留在project_1/ 路径,从而将project_2/settings.py DATABASES设置为指向project_1/development.db

You can simply define the same database in DATABASES in your settings.py.您可以简单地在 settings.py 中的DATABASES中定义相同的数据库。 So, if your database is PostgreSQL, you could do something like this:所以,如果你的数据库是 PostgreSQL,你可以这样做:

# 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.请注意,两个数据库用户( project_1_userproject_2_user )都应该对您希望使用的数据库具有适当的权限。 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 .因此,例如,如果project_1_app1project_2_app1做相同(或相似)的事情,它们似乎可以改为单个可重用的 app

Edit编辑

Since you use sqlite3, you should ensure the path you use is the same.由于您使用sqlite3,您应该确保您使用的路径相同。 So, assuming that project_1 and project_2 are siblings, like so:因此,假设project_1project_2是兄弟姐妹,如下所示:

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. project_2显然依赖于project_1的数据库。

In any case, perhaps, you should also take a look at the os.path module for more info.无论如何,也许您还应该查看os.path模块以获取更多信息。

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.您只需要在类 meta 中的模型中声明属性 db_table,其名称与 app + 模型(由 Django 自动生成)的名称不同,这两个项目需要相同的模型。 before the run makemigrations and migrate.在运行 makemigrations 和 migrate 之前。

class MyModel(models.Model):
    class Meta:
        db_table = 'MyModel'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个 Django 项目可以共享同一个数据库吗? - Can two Django projects share the same database? 如何在共享同一数据库的两个 Django 项目中使用 django-celery-beat 定期任务 - How to use periodic tasks with django-celery-beat in two Django projects sharing the same database 在两个不同的 Django 项目之间共享模型 - Share Models Between Two Different Django Projects 有没有办法在两个 django 项目之间共享一个公共数据库表而不伪造它? - Is there a way to share a common table of database between two django projects without faking it? 如何使Selenium和django使用同一个数据库 - How to make Selenium and django use the same database 如何使两个函数共享相同的非全局变量(Python) - How to make two functions share the same non global variable (Python) 如何使用matplotlib使两个标记在图例中共享相同的标签? - How to make two markers share the same label in the legend using matplotlib? 如何在 Django 中为同一个应用程序创建两个 url? - How to make two urls to the same app in Django? 如何链接两个项目 django - How to link two projects django 在两个Django项目之间共享数据库时如何解决“表“auth_permission”已经存在”错误 - How to solve "table "auth_permission" already exists" error when the database is shared among two Django projects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM