Django Database Connectivity

The file contains all the project settings along with database connection details. By default, Django works with SQLite, database and allows configuring for other databases as well.

Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc.

To connect with MySQL, django.db.backends.mysql driver is used to establishing a connection between application and database. Let's see an example.

We need to provide all connection details in the settings file. The file of our project contains the following code for the database.

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoApp',

After providing details, check the connection using the migrate command.

$ python3 migrate

This command will create tables for admin, auth, contenttypes, and sessions. See the example.

django database connectivity

Now, access to the MySQL database and see the database from the list of databases. The created database contains the following tables.

django database connectivity 1
Note: It throws an error if database connectivity fails: django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

Migrating Model

Well, till here, we have learned to connect Django application to the MySQL database. Next, we will see how to create a table using the model.

Each Django's model is mapped to a table in the database. So after creating a model, we need to migrate it. Let's see an example.

Suppose, we have a model class Employee in the file that contains the following code.


from django.db import models
class Employee(models.Model):
    eid      = models.CharField(max_length=20)
    ename    = models.CharField(max_length=100)
    econtact = models.CharField(max_length=15)
    class Meta:
        db_table = "employee"

Django first creates a migration file that contains the details of table structure. To create migration use the following command.

$ python3 makemigrations
django database connectivity 2

The created migration file is located into migrations folder and contains the following code.

from django.db import migrations, models
class Migration(migrations.Migration):
    initial = True
    dependencies = [
    operations = [
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('eid', models.CharField(max_length=20)),
                ('ename', models.CharField(max_length=100)),
                ('econtact', models.CharField(max_length=15)),
                'db_table': 'employee',

Now, migrate to reflect the changes into the database.

$ python3 migrate
django database connectivity 3

Check the database again, now it contains the employee table.

django database connectivity 4

See, a table is present in the database. Well, we have successfully established a connection between our Django application and MySQL database.

Related Tutorial
Follow Us #
Contents +