/    /  Django Databases

Django Databases

As we know the Django is a Framework that help us to implement the MVT pattern which contains the ORM feature i.e., Object Relational Mapper

Object Relational Mapper helps in communicating with relational databases using object oriented way rather than using SQL Queries.

So, What is the advantage here ?

A programmer is not required to write the SQL queries to any of the transactions rather it can be done using ORM approach.

Below are the databases that are being supported officially by Django

1. SQLite

2. MySQL

3. PostgreSQL

4. Oracle

5. MariaDB

Apart from the officially supported databases, below are certain 3rd party databases that allow you to work with Django

1. IBM DB2

2. Microsoft SQL Server

3. Firebird

4. ODBC

We can use the Mongo Database with the help of the libraries like MongoEngine, Djongo.

Inorder to work with the databases, you need to setup the databases first.

How to setup a database in Django?

Inorder to setup a database, you need to update the configuration in the settings.py file by updating the parameter DATABASES. The default connection is for

SQLite3 database that you can observe in the below settings.

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

db.sqlite3 is the default flat file that will be created in the Django’s project BASE_DIR location itself. SQLite do not require any additional credentials for this setup.

From the above configuration we can clearly understand the it takes a dictionary of key value pairs, where Keys are Engine and Name.

ENGINE is the most important key and associated values related with different databases are listed below.

SQLite :   django.db.backends.sqlite3

This is default database that is shipped with Django framework.

MySQL: django.db.backends.mysql

MySQL has several storage engines. You can always change the default storage engine in the server configuration.

MySQL’s default storage engine is InnoDB. You required to install this by using  “pip install PyMySQL”

PostgreSQL: django.db.backends.postgresql_psycopg2

You required to install this by using  “pip install psycopg2”

Oracle: django.db.backends.oracle

You required to install this by using  “pip install cx_Oracle”

MariaDB: To use MariaDB, use the MySQL backend, which is shared between the two

Installing the libraries to support the 3rd party databases that allow you to work with Django

IBM DB2: ibm_db_django

You required to install this by using  “pip install ibm_db_django”

Firebird: firebird

You required to install this by using  “pip install django-firebird”

From repository
git clone git://github.com/maxirobaina/django-firebird.git
cd django-firebird
sudo python setup.py install

ODBC (MSSQL Server): sql_server.pyodbc

You required to install this by using “pip install django-pyodbc”

or

“pip install django-pyodbc-azure” and can be used as Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc

Sample Configuration Example:

DATABASES = {
   'default': {
       'ENGINE': "django_pyodbc",
       'HOST': "127.0.0.1,1433",
       'USER': "mssql_user",
       'PASSWORD': "mssql_password",
       'NAME': "database_name",
       'OPTIONS': {
           'host_is_server': True
       },
   }
}

While configuring the Django database connection certain parameters change based on database type.

 Connection parameters:

ATOMIC_REQUESTS: Default value is “False”.

AUTOCOMMIT: Default value is “True”.

CONN_MAX_AGE: Database connection lifetime mentioned in seconds. By default it is “0” where it closes the DB connection for each request.

                “None” for unlimited persistent connections.

ENGINE:  This takes the database engine name.

HOST: This takes a database host, where an empty string means localhost. This will change based on the Database type.

NAME: The name of the database to use. For example, if SQLite, it’s the full path to the database file.

OPTIONS: This takes the key-value pairs that are specific to respective databases.

PASSWORD: password for connecting the database.

PORT: An empty string means the default port.

TIME_ZONE: Storing the date times in a database.

USER: Username used for database account.

Once after the database configuration is set, we are good to go ahead with migrations.