Skip to main content

Backend Development

Build a Django Task Tracker, Part 1: Models and Migrations

Create a separate Django project, model task ownership, and apply your first migration.

Build a small Django application that does one thing well: let a signed-in user keep a private list of tasks. This four-part series follows the same project from its data model to a tested HTTPS deployment. It assumes you can write Python functions and use a terminal; it explains the Django pieces as they appear.

The finished application supports creating tasks, marking them complete, and viewing a paginated list. Ownership is part of every read and write. We deliberately keep the interface server-rendered so you can trace a request without also learning a frontend build system.

Get the runnable project

Download the complete task-tracker source. Extract the archive and open the django-task-tracker folder. The download contains the finished code, migrations, templates, and tests. Follow the articles in order to understand each layer; you do not need to copy snippets into your CodeEasy installation.

Use Python 3.12 or newer. This version of the example uses Django 6.0.6. Create a virtual environment from the extracted project directory:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python -m django --version

On Windows, use .venv\Scripts\activate instead of the source command. If the version command cannot import Django, check that your terminal is using the virtual environment's Python. Install packages through python -m pip so the installer and interpreter agree.

Understand the directory boundary

manage.py is the command entry point. config/ contains site settings and root URLs. tasks/ contains the application model, forms, views, and tests. templates/ contains the HTML. SQLite writes to db.sqlite3 by default. None of these files uses the live CodeEasy database.

If you prefer to create the scaffolding yourself in a separate empty directory, django-admin startproject config . and python manage.py startapp tasks create the basic packages. Add tasks to INSTALLED_APPS, then use the matching files from the download as you follow each chapter. The downloadable project is the reference for the complete configuration.

Model a task around its owner

The model is short because it describes the stored facts rather than the page layout:

tasks/models.py

from django.conf import settings
from django.db import models


class Task(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='tasks')
    title = models.CharField(max_length=160)
    completed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at', '-pk']

    def __str__(self):
        return self.title

A foreign key stores which user owns each task. Referencing settings.AUTH_USER_MODEL follows the configured user model instead of hard-coding the built-in class. CASCADE means deleting that user also removes their tasks; this is a product decision, not a permission check.

completed starts false. created_at is assigned on insertion. The ordering includes the primary key as a tie-breaker, which matters when pagination needs a deterministic order. Two tasks can legitimately share the same title, so the title is not unique.

A database foreign key does not stop a view from returning another person's tasks. We will add that boundary explicitly in part two. Likewise, a maximum-length model field is not a substitute for validating form input before saving.

Apply the schema and create a local account

The finished project includes tasks/migrations/0001_initial.py. Run:

python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

migrate creates the auth, session, and task tables. createsuperuser prompts for credentials; do not reuse a production password in a tutorial. Open /admin/ and add a task, selecting your local account as its owner. The task model is registered in tasks/admin.py for this purpose.

If you changed the model while building from scratch, generate a migration with python manage.py makemigrations tasks before applying it. Committing model code without its migration leaves a new installation unable to build the expected table.

Checkpoint and a small exercise

In the Django shell, inspect your task's owner_id, completed, and created_at. Create a second task with the same title and confirm that both rows exist. Try to explain which fields are values on the task and which field refers to another table.

Do not create a separate model for “completed tasks.” Completion is a state of the same record. As an exercise, design a due-date field on paper: should it be optional, should it store a date or a timestamp, and how would old rows behave? Make that decision before changing the schema.

Next, build the private task list and form. For background, see Django's models documentation and first-project tutorial.

Stay curious

A new idea for your inbox.

Occasional updates when we publish new guides. No noise.

Subscribing...