0% found this document useful (0 votes)
6 views3 pages

Django MySQL Guide

This document provides a step-by-step guide to connect a Django project to a MySQL database, including installation of the MySQL client, configuration of the settings.py file, and running migrations. It also includes example model code for a Student and Course, as well as a list of default Django tables created for user authentication and session management. Following these steps will enable the Django application to utilize MySQL as its database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Django MySQL Guide

This document provides a step-by-step guide to connect a Django project to a MySQL database, including installation of the MySQL client, configuration of the settings.py file, and running migrations. It also includes example model code for a Student and Course, as well as a list of default Django tables created for user authentication and session management. Following these steps will enable the Django application to utilize MySQL as its database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

STEP-BY-STEP GUIDE: Connect Django to MySQL

1. Install MySQL Client

Run this in your terminal:

pip install mysqlclient

2. Configure settings.py in Django project:

Replace the DATABASES section with:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql',

'NAME': 'your_database_name',

'USER': 'your_mysql_username',

'PASSWORD': 'your_mysql_password',

'HOST': 'localhost',

'PORT': '3306',

3. Create MySQL Database:

Use MySQL Workbench or terminal to create the database before running migrations.

4. Run Migrations:

python manage.py makemigrations

python manage.py migrate


5. Run the server and it will use MySQL as the database.

-------------------------

models.py Example Code:

from django.db import models

class Student(models.Model):

name = models.CharField(max_length=100)

email = models.EmailField(unique=True)

age = models.IntegerField()

joined_date = models.DateField(auto_now_add=True)

class Course(models.Model):

title = models.CharField(max_length=100)

description = models.TextField()

credits = models.IntegerField()

-------------------------

Default Django Tables Created:

1. auth_user - Stores users info

2. auth_group - User groups

3. auth_permission - Permissions

4. django_admin_log - Admin actions logs


5. django_content_type - Links models with permissions

6. django_migrations - Tracks applied migrations

7. django_session - Stores session data

8. django_site - Site framework (optional)

9. django_auth_group_permissions - Links groups and permissions

10. django_auth_user_groups - Links users and groups

11. django_auth_user_user_permissions - Links users and permissions

These tables are essential for authentication, session management, and admin functionalities.

You might also like