MySQL with Django: A Comprehensive Guide 🎯

beginner
14 min

MySQL with Django: A Comprehensive Guide 🎯

Welcome to our deep dive into using MySQL with Django! This tutorial is designed for both beginners and intermediates, so let's get started. πŸš€

Understanding the Connection: MySQL and Django πŸ“

MySQL is a popular open-source Relational Database Management System (RDBMS), while Django is a high-level Python web framework. In this tutorial, we will learn how to integrate MySQL with Django for your web applications.

Installing MySQL for Django πŸ’‘

Before we dive in, make sure you have MySQL installed on your system. If not, follow the official MySQL installation guide.

Once MySQL is installed, we will install the psycopg2 library, which allows Django to interact with MySQL databases.

bash
pip install psycopg2-binary

πŸ“ Note: We will use the psycopg2 library in place of the default PostgreSQL library.

Configuring Django to Use MySQL βœ…

Now, let's configure Django to use MySQL.

Step 1: Create a New Django Project πŸ’‘

Start by creating a new Django project:

bash
django-admin startproject my_django_project

Step 2: Configure Django to Use MySQL πŸ“

Open the settings.py file in your project directory and make the following changes:

  1. Import psychog2:
python
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', ... } }
  1. Change 'django.db.backends.postgresql' to 'psycopg2':
python
DATABASES = { 'default': { 'ENGINE': 'psycopg2', 'NAME': 'your_database_name', 'USER': 'your_database_user', 'PASSWORD': 'your_database_password', 'HOST': 'localhost', 'PORT': '', } }

πŸ“ Note: Replace 'your_database_name', 'your_database_user', and 'your_database_password' with your MySQL database credentials.

Step 3: Create a New App πŸ’‘

Now, create a new app within your project:

bash
python manage.py startapp my_app

Step 4: Migrate the Database πŸ“

After creating the app, migrate the database to create the necessary tables:

bash
python manage.py makemigrations python manage.py migrate

Creating a Model and Querying Data βœ…

Now, let's create a simple model and query data from the database.

Step 1: Create a Model in my_app/models.py πŸ’‘

python
from django.db import models class MyModel(models.Model): name = models.CharField(max_length=200) description = models.TextField() def __str__(self): return self.name

Step 2: Register the Model πŸ“

Next, register the model in the admin.py file of your app:

python
from django.contrib import admin from .models import MyModel admin.site.register(MyModel)

Step 3: Run the Server and Access the Admin Site πŸ“

Finally, run the server and access the admin site to create and manage instances of your model:

bash
python manage.py runserver

Now, open your web browser and navigate to http://127.0.0.1:8000/admin/. You should see your new model listed, and you can create instances of MyModel.

Quiz πŸ’‘

Quick Quiz
Question 1 of 1

Which library allows Django to interact with MySQL databases?

That's it for this section! In the next part, we will dive deeper into advanced topics like raw SQL queries, managing relationships, and more. Stay tuned! 🎯