How to connect MySQL to Django

Django provides a default database called SQLite which is an inbuilt database. But sometimes we might choose to connect popular databases such as MySQL to the Django framework to make it more interactive. Therefore, in this tutorial, we will learn how to connect MySQL database to the Django framework using the below-mentioned steps.

Step 1: Install the mysqlclient package of Python

The sqlclient module provides an interface between the Python code and the MySQL database. To install mySQL on the local computer execute the following command:

pip install mysqlclient

pip install mysqlclient

Step 2: Create a new Project  and App

Next, we will create a new project called Project1 in Django by running the following command:

django-admin startproject Project1

Thus a new project is created in Django with the required files and configurations. We will also change the current directory to Project1 by running the command mentioned below:

cd Project1

Inside the project we will create a new app called projectApp using the following command:

python manage.py startapp projectApp

Step 3: Create a database

We will then create a database called ‘mydb‘ in MySQL by executing the following instruction in MySQL:

CREATE DATABASE mydb;

To see the databases that are created we can run the following instruction:

show databases;

Create a database in MySQL with Django

Step 4: Change settings.py to point to the desired database

Next, we will change the settings.py by adding the following mentioned code in the database section of the file.

DATABASES = {
    'default': {
       'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '1234',
        'HOST':'localhost',
        'PORT':'3306',
    }
}

Since we are shifting from SQLite database to MySQL database, we change the engine from  ‘django.db.backends.sqlite3’ to ‘django.db.backends.mysql‘. Apart from this, the key values of the database dictionary are:

  • NAME:- This is the name of the database we are connecting to.
  • USER:- This is the username of the user who is currently manipulating the database.
  • PASSWORD: This is the password set by the user for the database.
  • HOST: By default, we are connected to localhost and the port number is 3306. This port number is used by mysqlclient to connect with the MySQL database.

Step 5: Create a sample model

We will create a sample model that we will later migrate to our database. Models in Django provides a framework for the database by providing tables, field, and constraints. We will create a sample model called customers in the models.py file of our project.

from django.db import models
class Customer(models.Model):
    name= models.CharField(max_length=200,blank=True,null=True)
    email=models.EmailField(max_length=500,blank=True,null=True)
    username=models.CharField(max_length=200,blank=True,null=True)
    location=models.CharField(max_length=200,blank=True,null=True)

Step 6: Perform migrations to the database

The new model created must be transferred to the database on the MySQL server. We will perform migrations to the database by executing the following command:

python manage.py makemigrations

Perform migrations to the database
Next, we run the command to perform the migrations:

python manage.py migrate

python manage.py migrate

python manage.py sqlmigrate projectApp 0001

python manage.py sqlmigrate projectApp 0001

Displaying the table created in the database

The table is created in the database and to display it, we run the following command:

select * from projectapp_customer;

Show MySQL table in Django

Thus we have reached the end of this tutorial on how to connect MySQL database to Django. To read more about Django click on the following link: Django – Submit Form data with Post Method

Leave a Reply

Your email address will not be published. Required fields are marked *