Skip to content

Add task model

Serhii Horodilov edited this page Feb 20, 2024 · 6 revisions

At the end of this guide you will:

  • create a Django model to represent tasks
  • create and apply migrations to the project database
Make sure you have successful configured PostgreSQL database before you begin.

Table of Contents

Getting started

git checkout bp-models-tasks

Task model UML diagram

erDiagram
    task {
        UUID uuid "primary key"
        string summary
        string description
        bool completed
        datetime created_at
        datetime updated_at
        bigint reporter "foreign key"
        bigint assignee "foreign key"
    }
Loading

Guide

Create task model

# tasks/models.py
"""
Tasks application models

"""

import uuid

from django.contrib.auth import get_user_model
from django.db import models

UserModel = get_user_model()


class TaskModel(models.Model):
    """
    Task model implementation

    :ivar uuid: primary key
    :type uuid: :class: `uuid.UUID`
    :ivar summary: title, or short description (up to 128 characters).
    :type summary: str
    :ivar description: detailed description, defaults to None.
    :type description: str
    :ivar completed: completed status, defaults to False.
    :type completed: bool
    :ivar created_at: created timestamp, non-editable.
    :type created_at: :class: `datetime.datetime`
    :ivar updated_at: updated timestamp, non-editable.
    :type updated_at: :class: `datetime.datetime`
    :ivar assignee: reference to a user that task is assigned to.
    :type assignee: :class: `UserModel`, optional
    :ivar reporter: reference to a user that created the task.
    :type reporter: :class: `UserModel`

    Represents a tasks registered in the system.
    Each new task is created as uncompleted by default.
    Each task gets its ``created_at`` timestamp once on task creation.
    Each task updates its ``updated_at`` timestamp on each task save.

    """

    uuid = models.UUIDField(
        default=uuid.uuid4,
        editable=False,
        primary_key=True,
        verbose_name="primary key",
    )

    summary = models.CharField(
        max_length=128,
        help_text="Required. 128 characters or fewer."
    )
    description = models.TextField(
        blank=True,
        default="",
    )
    completed = models.BooleanField(
        default=False,
        verbose_name="completed status"
    )

    # task metadata
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    # relationships
    assignee = models.ForeignKey(
        UserModel,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="tasks_assigned",
        verbose_name="assigned to",
    )
    reporter = models.ForeignKey(
        UserModel,
        on_delete=models.PROTECT,
        related_name="tasks_reported",
        verbose_name="reported by",
    )

    @property
    def title(self) -> str:
        return str(self)

    def __str__(self) -> str:
        """Return a string version of an instance"""

        return self.summary

Changes

  1. Full change log