DEV Community

Sarah Nyambura Kiiru
Sarah Nyambura Kiiru

Posted on

Day 3: Diving deeply into Models in Django.

Understanding Django Models in Depth with AboutMe and Skill

If you're new to Django, models are one of the core pillars you'll work with. They allow you to define your database structure using Python code, without writing raw SQL.

In this article, we’ll break down two real-world Django models: AboutMe and Skill, used in a personal portfolio-style app.

What Is a Model?

In Django, a model is a Python class that maps to a database table. Each model class represents a table, and each attribute represents a column in that table.

about/models.py

Let’s look at two models:

AboutMe Model

class AboutMe(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False, default="Sarah Nyambura")
    bio = models.TextField(blank=False, null=False, default="A passionate Django learner exploring web development.")
    updated_on = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name
Enter fullscreen mode Exit fullscreen mode

Breakdown of what the code is about:

Field Type Purpose
name CharField Stores the user's name. Max length of 100. Cannot be blank or null.
bio TextField Stores a longer description (bio).
updated_on DateTimeField Automatically updates the timestamp whenever this record is saved.

Skill Model

class Skill(models.Model):
    title = models.CharField(max_length=100, blank=False, null=False, default="Django Web Development")
    about = models.ForeignKey(AboutMe, on_delete=models.CASCADE, related_name="skills")

    def __str__(self):
        return self.title
Enter fullscreen mode Exit fullscreen mode

Immm

what the code is about:

Field Type Purpose
title CharField Name of the skill (e.g., Python, HTML).
about ForeignKey A many-to-one link to the AboutMe model. One person (AboutMe) can have many skills.
  • on_delete=models.CASCADE: If the AboutMe profile is deleted, all related skills will be deleted.
  • related_name="skills": Allows you to access skills from an AboutMe instance using .skills.all().

Real-World Usage in my app

The profile says:

  • Name: Sarah Nyambura
  • Bio: A passionate Django learner
  • Skills: Django Web Development, Python, HTML/CSS

This is exactly how your models represent one in code and store it in the database.

Why Models Matter

  • They are the foundation of your data structure.
  • Django handles the database for you.
  • You can use Python to create, update, delete, and query data.

After Creating Models

Don't forget to make and apply migrations:

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Connecting Models with Views, Templates, and URLs

View: Fetch Data from the Model

desc

To display this data in your browser, connect the view to a URL.

descrn

still route the main url of the project

Immmmg

Template: Display It with HTML

Ion

this how the output is after starting the developer server

Imaon

As someone who’s just getting started with Django, working with models felt intimidating at first.
But after building these two AboutMe and Skill I now realize how powerful and structured Django can be. Models allow me to describe the kind of data I want to store without touching SQL at all.
It feels like I’m shaping my website with simple Python classes, and that’s pretty awesome.

Top comments (0)