9đź‘Ť
If you simply need multiple connections, you can do something like this:
from django.db import load_backend
myBackend = load_backend('postgresql_psycopg2') # or 'mysql', 'sqlite3', 'oracle'
myConnection = myBackend.DatabaseWrapper({
'DATABASE_HOST': '192.168.1.1',
'DATABASE_NAME': 'my_database',
'DATABASE_OPTIONS': {},
'DATABASE_PASSWORD': "",
'DATABASE_PORT': "",
'DATABASE_USER': "my_user",
'TIME_ZONE': "America/New_York",})
# Now we can do all the standard raw sql stuff with myConnection.
myCursor = myConnection.cursor()
myCursor.execute("SELECT COUNT(1) FROM my_table;")
myCursor.fetchone()
6đź‘Ť
This will be in Django 1.2.
See http://docs.djangoproject.com/en/dev/topics/db/multi-db/
- How to access user names and profiles with django-allauth
- How to have Accent-insensitive filter in django with postgres?
4đź‘Ť
The most recent discussion I’ve seen on it was in the Proposal: user-friendly API for multi-database support django-developers thread, which also has an example of one way to use multiple databases using Managers in the original message.
- How to check that an uploaded file is a valid Image in Django
- Extending Django's Generic Views
- How to test Django custom model fields?
- Installed pytest but running `pytest` in bash returns `not found`
3đź‘Ť
If you read a few of the many (many) threads on this subject in django-dev, you will see that what looks straightforward, isn’t. If you pick a single use case, then it looks easy, but as soon as you start to generalize in any way you start to run into trouble.
To use the above-referenced thread as an example, when you say “multiple databases”, which of the following are you talking about?
- All DB on the same machine under the same engine.
- All DB on same machine, different engines (E.g. MySQL + PostgreSQL)
- One Master DB with N read-only slaves on different machines.
- Sharding of tables across multiple DB servers.
Will you need:
- Foreign keys across DBs
- JOINs across machines and/or engines
- etc. etc.
One of the problems with a slick ORM like Django’s is that it hides all of those messy details under a nice paint job. To continue to do that, but to then add in any of the above, is Not Easy ™.
2đź‘Ť
Eric Florenzano wrote a very good blog post that allows you some multiple database support at: Easy MultipleDatabase Support for Django.
It starts by creating a new custom manager that allows you to specify the database settings.
- How to fix Error: pg_config executable not found on Elastic Beanstalk permanently
- Live notification/chat in django
- Django – How can you include annotated results in a serialized QuerySet?
- Pytest and Django settings runtime changes
- Google App Engine logs a mess of New connection for … and Client closed local connection on
2đź‘Ť
Multiple database to choose from
We always need one named default, the names of the rest are up to you.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mupltiple_datab_app1',
'USER': 'root',
'PASSWORD': 'admin',
'HOST': "",
'PORT': "",
},
'user1':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mupltiple_datab_app2',
'USER': 'root',
'PASSWORD': 'admin',
'HOST': "",
'PORT': "",
},
'user2':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mupltiple_datab_app3',
'USER': 'root',
'PASSWORD': 'admin',
'HOST':"" ,
'PORT': "" ,
}
}
for sync to one particular database
manage.py syncdb --database=user1
- Django WeasyPrint CSS integration warning: Relative URI reference without a base URI: <link href="/static/css/bootstrap.min.css"> at line None
- Newline in label for Django form field
- Celery and signals
1đź‘Ť
There is a “using” directive for queries,saves, and deletes
https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database
- How to test postgresql credentials from bash?
- Django StaticFiles and Amazon S3: How to detect modified files?
- Django: use render_to_response and set cookie
- Render ChoiceField options in Django template
- Django ModelChoiceField: how to iter through the instances in the template?
0đź‘Ť
I think you will have to resort to “raw sql” .. kinda thing ..
look here: http://docs.djangoproject.com/en/dev/topics/db/sql/
you need a “connection” to your other database,
if you look at django/db/__init__.py
around line 39 (in my version ..)
connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
try to take it from there ..
P.S. I haven’t really tried this or anything .. just trying to point in the general direction of what I think might solve your problem.
0đź‘Ť
Eric Florenzano’s approach works well if all your databases use the same engine. If you have different engines (Postgres and MSSQL in my case) you will run into many issues deep in the ORM code (such as models/sql/where.py using the default connection’s SQL syntax).
If you need this to work, you should wait for Alex Gaynor’s MultiDB project which is planned for Django 1.2
- Recursive crawling with Python and Scrapy
- Python – pyodbc call stored procedure with parameter name
- Django python-rq — DatabaseError SSL error: decryption failed or bad record mac
- Django DateTimeField says 'You are 5.5 hours ahead of server time.'
0đź‘Ť
From Django 1.2, it will support multiple databases. See: http://docs.djangoproject.com/en/dev/topics/db/multi-db/
Version 1.2 is now in beta