Django Tutorial #5: Understanding Models and Databases in Django | A Beginner's Guide
Welcome back to Part 5 of my Django Tutorial Series! 🚀
In the previous tutorial, we explored Django Templates and learned how to display dynamic information on web pages.
But have you ever wondered where a website stores information such as usernames, passwords, products, and student records?
This is where Django Models and Databases come into the picture.
In this tutorial, we will understand how Django stores, retrieves, and manages application data.
1. What Is a Database?
A database is an organized collection of information that can be stored, accessed, updated, and managed easily.
For example, a student management system may store:
- Student names
- Roll numbers
- Email addresses
- Course details
- Marks
Django supports several databases, including SQLite, PostgreSQL, and MySQL.
2. What Are Django Models?
A Model is a Python class that represents the structure of a database table.
Each model generally represents a table, while its fields represent columns.
For example, a Student model can contain:
- Name
- Roll number
Django uses its Object-Relational Mapping (ORM) system to interact with databases using Python code.
3. Creating Your First Django Model
Open the models.py file inside your Django application.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
roll_number = models.IntegerField()
def __str__(self):
return self.name
Explanation:
-
models.Modelmakes Student a Django model. -
CharFieldstores text. -
EmailFieldstores email addresses. -
IntegerFieldstores integer values. -
__str__()provides a readable representation of an object.
4. Understanding Django Migrations
After creating a model, we need to apply its structure to the database.
Django provides two important migration commands.
Step 1: Make migrations
python manage.py makemigrations
This creates migration files based on changes in your models.
Step 2: Apply migrations
python manage.py migrate
This applies the migration operations to the database.
5. Registering Models in Django Admin
Django provides a built-in administration interface.
To manage our Student model through the admin panel, open admin.py.
from django.contrib import admin
from .models import Student
admin.site.register(Student)
Create an administrator account:
python manage.py createsuperuser
Then start the server:
python manage.py runserver
Visit /admin/ and log in with your superuser credentials.
You can now manage student records through Django Admin.
6. Understanding Django ORM
ORM stands for Object-Relational Mapping.
It allows developers to interact with database records using Python instead of writing SQL queries for every operation.
Some useful ORM operations include:
Create a record
Student.objects.create(
name="Rahul",
email="rahul@example.com",
roll_number=101
)
Retrieve all records
students = Student.objects.all()
Retrieve a particular record
student = Student.objects.get(id=1)
Update a record
student = Student.objects.get(id=1)
student.name = "Aman"
student.save()
Delete a record
student = Student.objects.get(id=1)
student.delete()
7. Why Are Django Models Important?
- They organize application data.
- They simplify database operations.
- They reduce repetitive SQL coding.
- They support database relationships.
- They work with Django's migration system.
- They make data management easier.
8. Conclusion
Django Models and Databases are essential for building data-driven web applications.
Models define how information is structured, while Django ORM makes it easier to perform database operations using Python.
Understanding these concepts helps developers build applications such as student management systems, e-commerce websites, dashboards, and more.
What's Next?
In Django Tutorial #6, we will learn about Django Forms and how to collect and validate user information.
What kind of Django application are you currently building?
Share your thoughts in the comments!
Happy Coding! 💜
— SP Randhawa
Top comments (1)
🚀 Welcome to Part 5 of my Django Tutorial Series!
This time, we explored Django Models, Databases, Migrations, and ORM.
Understanding how data is stored and managed is an important step in building real-world applications.
💡 Which database do you prefer working with: SQLite, MySQL, or PostgreSQL?
Share your experience in the comments!
Happy Learning! 💜