Django:如何使用多个数据库? [英] Django : how to use multiple databases?

查看:55
本文介绍了Django:如何使用多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django构建网站,并且我的 User 模型中需要具有GPS坐标属性.

I'm building a website with Django and I need to have a GPS coordinate attribute in my User model.

我将使用需要PostgreSQL的GeoDjango,并且由于我的网站已经在使用sqlite3默认数据库,因此我需要在项目中安装PostgreSQL数据库才能创建 Coordinates 模型并将其链接到SQLite数据库的 User 模型.

I will be using GeoDjango, which needs PostgreSQL and, since my website is already working with the sqlite3 default database, I need to install a PostgreSQL database to the project in order to create a Coordinates model in it and link it to the User model of the SQLite database.

我在Internet上找到了很多有关如何使用多个数据库" 的教程,但是它们都是非常基础的.由于我是Django和Web编程的新手,因此我需要有关多个数据库的深入教程.我有几个问题,如果您能回答其中一个或多个问题,我先谢谢您!

I found lots of tutorials on the internet about "how to use multiple databases" but they are all very basics. Since I'm quite new to Django and web programming, I'm needing an in-depth tutorial about multiple databases. I have a few questions and I thank you in advance if you can answer one or more of those!

注意:我使用的是Windows 10,同时已安装PostgreSQL和Windows的ubuntu应用.

1)如何在Django项目中为新数据库创建和初始化文件?db.sqlite3文件是在Django项目初始化时自动创建的,那么如何创建另一个文件?

1) How can I create and initialize the file for the new database in the Django project? The file db.sqlite3 was automatically created with the Django project initialization so how can I create another one?

2)是否需要使用新的GPS坐标应用程序?还是可以使用现有的应用程序?

2) Do I need to use a new app for the GPS coordinates or can I use an existing one?

3)如何在我的 User 模型和将在另一个数据库中的 Coordinates 模型之间建立 OneToOne 关系?/p>

3) How can I make the OneToOne relationship between my User model and my Coordinates model that will be in the other database?

推荐答案

@NalinDobhal在评论中提到:

As @NalinDobhal mentions in the comments:

跨数据库关系

Django当前不支持跨多个数据库的外键或多对多关系.如果您使用路由器将模型分区到不同的数据库,则任何外键和许多这些模型定义的多对多关系必须在单个数据库内部.

Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.

据我所见,您有2个选择:

As I see it you have 2 options:

  1. 安装 SpatiaLite ,并继续在项目中使用SQLite,以启用空间类型在您现有的数据库中(遵循有关此问题的记录的GeoDjango说明)
  2. 首选解决方案::将现有的SQLite数据库迁移到PostgreSQL并启用PostGIS.您可以很好地了解为什么首选此方法,以及如何通过Django
  1. Install SpatiaLite and continue using SQLite for your project, enabling spatial types in your existing DB (follow the documented GeoDjango Instructions on the matter)
  2. Preferred Solution: Migrate your existing SQLite DB to PostgreSQL and enable PostGIS. You can have an excellent read on why this is preferred and how to do the migration correctly through Django in this article.

长话短说,是首选流程:

Long story short for the preferred process:

  • 对现有数据库进行数据库转储:

  • Make a DB dump of the existing DB:

python manage.py dumpdata > datadump.json

  • 输入Django Shell并删除现有的 ContentType 数据

    python manage.py shell
    
    >>> from django.contrib.contenttypes.models import ContentType
    >>> ContentType.objects.all().delete()
    >>> quit()
    

  • 将转储文件加载到PostgreSQL数据库中:

  • Load the dump file into the PostgreSQL DB:

    python manage.py loaddata datadump.json
    

  • 注意:此迁移过程不仅是特定于PostgreSQL的SQLite,而且可用于数据库之间的几乎所有迁移(我知道ATM).

    Note: This migration process is not only SQLite to PostgreSQL specific and can be used in almost every migration between DBs (that I know of ATM).

    这篇关于Django:如何使用多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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