WEEK 9 – THURSDAY
TOPIC: Creating a Database Table in Django Using MySQL
Sub-topic: Creating a Student Table with Django Models and Migrations
What We Are Doing in This Practical
We already have:
A Django project
A Django app called
studentsA MySQL database called
student_portalDjango connected to MySQL in
settings.py
Now, we want to create a Student table inside the student_portal database.
The table will store:
Student full name
Student email
Student course
STEP 1: Open the models.py File
Go to this file:
students/models.py
What we are doing
We are opening the file where Django allows us to describe a database table using Python code.
In Django, we do not need to first create the table manually in MySQL. We create a model in models.py, and Django uses it to create the table for us.
STEP 2: Import Django Database Tools
Inside students/models.py, write:
from django.db import models
What we are doing
We are bringing Django’s database tools into our file.
We need these tools because they help us create:
Tables
Columns
Text fields
Email fields
Number fields
Date fields
Meaning of the code
| Code | Meaning |
|---|---|
from | Take something from another place |
django | The Django framework |
.db | Django database section |
import | Bring a tool into this file |
models | Tools for creating tables and columns |
STEP 3: Create the Student Model
Under the import line, write:
class Student(models.Model):
What we are doing
We are creating a Django model called Student.
A model is like a blueprint for a database table.
Django will use this blueprint to create a table for students inside MySQL.
Meaning of the code
| Code | Meaning |
|---|---|
class | Create a blueprint/template |
Student | The name of our model |
models.Model | Tells Django that this is a database table |
: | Starts the code inside the model |
Table name Django will create
Because our app is called students and our model is called Student, Django will normally create:
students_student
STEP 4: Create the Full Name Column
Inside the Student model, write:
full_name = models.CharField(max_length=100)
What we are doing
We are creating a column called full_name.
This column will store each student’s name.
Examples:
Musa Ibrahim
Aisha Bello
John David
Meaning of the code
| Code | Meaning |
|---|---|
full_name | Name of the column |
= | Give this column a field type |
models.CharField | Store short text |
max_length=100 | Allow a maximum of 100 characters |
STEP 5: Create the Email Column
Write:
email = models.EmailField()
What we are doing
We are creating a column called email.
This column will store each student’s email address.
Examples:
musa@gmail.com
aisha@gmail.com
Meaning of the code
| Code | Meaning |
|---|---|
email | Name of the column |
= | Give this column a field type |
models.EmailField() | Store email addresses and check email format |
STEP 6: Create the Course Column
Write:
course = models.CharField(max_length=100)
What we are doing
We are creating a column called course.
This column will store the course each student is studying.
Examples:
Python Programming
Web Development
Data Analysis
Meaning of the code
| Code | Meaning |
|---|---|
course | Name of the column |
models.CharField | Store short text |
max_length=100 | Allow a maximum of 100 characters |
STEP 7: Save the Complete Model
Your complete students/models.py file should look like this:
from django.db import models
class Student(models.Model):
full_name = models.CharField(max_length=100)
email = models.EmailField()
course = models.CharField(max_length=100)
What we have done
We have created the blueprint for a student table.
At this point, the table is not yet inside MySQL. Django only knows what the table should look like.
STEP 8: Create a Migration File
Open your terminal inside the project folder where manage.py is located.
Run:
python manage.py makemigrations
What we are doing
We are telling Django:
“Look at my model and prepare instructions for creating the table.”
Django creates a migration file inside:
students/migrations/
You may see:
Migrations for 'students':
students/migrations/0001_initial.py
+ Create model Student
Meaning of the command
| Code | Meaning |
|---|---|
python | Run Python |
manage.py | Django command file |
makemigrations | Create instructions for database changes |
STEP 9: Send the Table to MySQL
Run:
python manage.py migrate
What we are doing
We are telling Django:
“Use the migration instructions and create the table inside the MySQL database.”
Django connects to the database named student_portal because that is the database name written in settings.py.
After this command, the students_student table is created inside MySQL.
Meaning of the command
| Code | Meaning |
|---|---|
python | Run Python |
manage.py | Django command file |
migrate | Apply the migration instructions to the database |
STEP 10: Check the Table in MySQL
Open MySQL Workbench.
Run:
USE student_portal;
What we are doing
We are selecting the database where our table was created.
Meaning of the code
| Code | Meaning |
|---|---|
USE | Select a database |
student_portal | The database name |
; | End the MySQL command |
STEP 11: Show All Tables
Run:
SHOW TABLES;
What we are doing
We are asking MySQL to show every table inside the student_portal database.
You should see:
students_student
You may also see Django tables such as:
auth_user
django_admin_log
django_migrations
django_session
STEP 12: Check the Columns in the Student Table
Run:
DESCRIBE students_student;
What we are doing
We are asking MySQL to show the columns inside the students_student table.
You should see columns similar to:
| Column | Meaning |
|---|---|
id | Automatic student number created by Django |
full_name | Student’s full name |
email | Student’s email |
course | Student’s course |
Final Summary
Create Student model in models.py
↓
Create migration with makemigrations
↓
Run migrate
↓
Django creates students_student table in MySQL
↓
Check the table in MySQL Workbench


0 comments:
Post a Comment