DEV Community

Cover image for Django Tutorial #5: Understanding Models and Databases in Django | A Beginner's Guide
S.P.Randhawa
S.P.Randhawa

Posted on

Django Tutorial #5: Understanding Models and Databases in Django | A Beginner's Guide

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
  • Email
  • 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
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • models.Model makes Student a Django model.
  • CharField stores text.
  • EmailField stores email addresses.
  • IntegerField stores 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
Enter fullscreen mode Exit fullscreen mode

This creates migration files based on changes in your models.

Step 2: Apply migrations

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Create an administrator account:

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

Then start the server:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

Retrieve all records

students = Student.objects.all()
Enter fullscreen mode Exit fullscreen mode

Retrieve a particular record

student = Student.objects.get(id=1)
Enter fullscreen mode Exit fullscreen mode

Update a record

student = Student.objects.get(id=1)
student.name = "Aman"
student.save()
Enter fullscreen mode Exit fullscreen mode

Delete a record

student = Student.objects.get(id=1)
student.delete()
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
sp_randhawa profile image
S.P.Randhawa •

🚀 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! 💜