Django : how to use multiple databases?

不打扰是莪最后的温柔 提交于 2020-07-09 14:34:09

问题


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

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.

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!

Note: I'm using windows 10 with PostgreSQL installed with the ubuntu app of windows.

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) Do I need to use a new app for the GPS coordinates or can I use an existing one?

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


回答1:


As @NalinDobhal mentions in the comments:

Cross-database relations

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.

As I see it you have 2 options:

  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
    
  • Enter the Django Shell and delete the existing ContentType data

    python manage.py shell
    
    >>> from django.contrib.contenttypes.models import ContentType
    >>> ContentType.objects.all().delete()
    >>> quit()
    
  • Load the dump file into the PostgreSQL DB:

    python manage.py loaddata datadump.json
    

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




回答2:


You can't just create a database file. except for SQL lite that is a very basic database and can not be used in production, other databases usually come with embedded servers and don't have a file you can see. you can actually use multiple databases either you have models in different apps or you need some specific tables to be in another database. but Django does not support cross-database relations!

see here for more info:

Django 1.11 can we create relationship between tables from two different databases?



来源:https://stackoverflow.com/questions/59859045/django-how-to-use-multiple-databases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!