I am using mongoengine with Django and my project needs to connect to one instances of MongoDB while another with sql .How my databse section of setting.py should be like ?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'admin_db',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
},
}
from mongoengine import connect
connect(
db='pom',
username='admin',
password='root',
host='mongodb://admin:root#localhost'
)
You could add multiple databases for your app in your settings.py like,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'admin_db',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
},
'your_desired_db_name' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'db_name'
}
For integration with mongodb, you may need to look up,
Django-nonrel
Django-MongoEngine
Also, you may need to look up Django documentation for multiple databases
MongoEngine does not support all Django contrib modules directly. If your projects dont need them (unlikely) you can use mongoengine directly. Otherwise you can also try
djongo
Which seems to work fine with the latest Django version.
Related
The django project has multiple apps and they all right now access the same DB. If I want one app which has only read queries to read from read replica will I have to add routers for both DB or creating one router for read replica and alloting it to the app will work? Is there a better way to do this?
you can use multiple database as defined in documentation:
https://docs.djangoproject.com/en/2.0/topics/db/multi-db/
Ex:
DATABASES = {
'default': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'password1'
},
'read_replica': {
'NAME': 'customer_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_cust',
'PASSWORD': 'password2'
}
}
After use a Database Router (django.db.router):
https://docs.djangoproject.com/en/2.0/topics/db/multi-db/#using-routers
There is a DATABASE_ROUTERS config as well.
I am working on a Django project where I need to use both mysql/postgresql as well as mongoDB, one as primary and one as secondary database. How do I configure my db settings to use two databases?
I am able to use 1 database as postgresql or mongoDB, but not able to use both. I have provided the code below of what I have tried.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': os.environ.get("DB_HOST", DB_HOST),
'PORT': os.environ.get('DB_PORT', DB_PORT),
'NAME': os.environ.get("DB_NAME", DB_NAME),
'USER': os.environ.get("DB_USER", DB_USER),
'PASSWORD': os.environ.get("DB_PASSWORD", DB_PASSWORD),
},
}
I have figured out the solution to this. We would need to use a third party plugin called Django MongoDB Engine.
Documentation: https://django-mongodb-engine.readthedocs.io/en/latest/topics/setup.html
'default': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'priv4te'
},
'mongo' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database'
}
}```
I am building a companion web/mobile application for an existing web app. The database is implemented in MySQL. I will be writing an API using Django/Django Rest Framework (DRF).
The application is used by various organizations. The interesting part about the implementation is that each organization has it's own database. In the existing web application, the user enters the database name along with the login credentials.
How to go about implementing this in Django? I am going to have lots of models - and they all need to read from the correct database depending on the current user.
in settings.py
DATABASES = {
'default': {},
'database_one': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'PORT': '3306',
'HOST': '127.0.0.1',
'USER': 'db1_user',
'PASSWORD': 'db1_pwd'
},
'database_two': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db2',
'PORT': '3306',
'HOST': '127.0.0.1',
'USER': 'db2_user',
'PASSWORD': 'db2_pwd'
}
}
For sync your database, you should do like this:
python manage.py migrate
python manage.py migrate --database=database_one
python manage.py migrate --database=database_two
The first command operates on the default database, and with the --database option you can operate the database you want.
More info. can be found here: https://docs.djangoproject.com/en/2.0/topics/db/multi-db/
What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'snackvoxadmin'
},
'users': {
.....
}
}
The database user has a url like similar to this one: postgres://a78adj1he81....
You can decompose your database url and configure it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
And the pattern for a database url is :
postgres://user:password#host:post/database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases
Or you can use the package dj-database-url to directly use the database url.
E.g. from readme :
import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.
How do I use a database connection URL in setting up database config in Django as opposed to using a dictionary?
Instead of using:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'PORT': os.getenv('DB_PORT'),
'HOST': os.getenv('DB_HOST')
}
}
I want to use:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgresql://DB_USER:DB_PASSWORD#localhost:5432/DB_NAME'
}
}
This is how I added a database url to one of my django apps.
First, install this package
pip install dj-database-url
Next, add this line to your settings.py in your django project
...
DATABASES['default'] = dj_database_url.config(default=os.getenv('DATABASE_URI'))
...
In place of os.getenv('DATABASE_URI'), you can also explicitly mention your database url
You can refer here to the github repo for more configurations.