Skip to main content

Overview

The Tareas model represents tasks in the system. Each task has a name and is assigned to a single team member. This model inherits from ModeloBase, which provides automatic timestamping and soft delete functionality.

Model Definition

class Tareas(ModeloBase):
    nombre_tarea = models.CharField(null=True, blank=True, max_length=100)
    encargados = models.ForeignKey(encargado, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.nombre_tarea

Fields

Direct Fields

nombre_tarea
CharField
The name or title of the task.Constraints:
  • Maximum length: 100 characters
  • Optional: null=True, blank=True - can be empty or null
encargados
ForeignKey
required
Reference to the team member assigned to this task.Relationship Details:
  • References: encargado model
  • On Delete: CASCADE - deleting the team member will delete the task
  • Required field (must be assigned to someone)
Despite the plural name “encargados”, this is a ForeignKey (one-to-one relationship), not a ManyToManyField. Each task is assigned to exactly one team member.

Inherited Fields (from ModeloBase)

fecha_registro
DateField
The date when the task was created.Constraints:
  • Automatically set on creation (auto_now_add=True)
  • Read-only after creation
  • Verbose name: “Fecha Registro”
hora_registro
TimeField
The time when the task was created.Constraints:
  • Automatically set on creation (auto_now_add=True)
  • Read-only after creation
  • Verbose name: “Hora Registro”
status
BooleanField
default:"True"
Indicates whether the task is active or deleted (soft delete).Constraints:
  • Default value: True (active)
  • Set to False for soft deletion

Model Relationships

Forward Relationships

encargados
encargado
Access the team member assigned to this task:
tarea = Tareas.objects.get(id=1)
assigned_member = tarea.encargados
print(assigned_member.nombres)

Reverse Relationships

tareas_set
QuerySet[Tareas]
From an encargado instance, access all tasks assigned to them:
member = encargado.objects.get(id=1)
tasks = member.tareas_set.all()
for task in tasks:
    print(task.nombre_tarea)
Since no related_name was specified, Django uses the default reverse relationship name: tareas_set

String Representation

The model’s __str__() method returns the task name:
def __str__(self):
    return self.nombre_tarea
Since nombre_tarea can be None, the string representation may return None for tasks without names.

Usage Examples

Creating a Task

from CTP.models import Tareas, encargado

# Get a team member to assign the task to
member = encargado.objects.get(id=1)

# Create a new task
tarea = Tareas.objects.create(
    nombre_tarea="Implement user authentication",
    encargados=member
)

Querying Tasks

# Get all active tasks
active_tasks = Tareas.objects.filter(status=True)

# Get tasks assigned to a specific team member
member = encargado.objects.get(id=1)
assigned_tasks = Tareas.objects.filter(encargados=member)
# Or using reverse relationship
assigned_tasks = member.tareas_set.all()

# Get tasks with names (excluding null/empty names)
named_tasks = Tareas.objects.exclude(nombre_tarea__isnull=True).exclude(nombre_tarea='')

Updating a Task

tarea = Tareas.objects.get(id=1)

# Update the task name
tarea.nombre_tarea = "Updated task description"
tarea.save()

# Reassign to a different team member
new_member = encargado.objects.get(id=2)
tarea.encargados = new_member
tarea.save()

Soft Deleting a Task

tarea = Tareas.objects.get(id=1)
tarea.status = False
tarea.save()

# Query only active tasks
active_tasks = Tareas.objects.filter(status=True)
Deleting an encargado will cascade delete all tasks assigned to them. Consider reassigning tasks before deleting a team member, or use soft delete.

Database Table

This model is stored in the database table: CTP_tareas

Design Considerations

Field Naming: The field encargados (plural) suggests multiple assignees, but it’s actually a ForeignKey that only allows one assignee per task. Consider this when working with the model.
Optional Task Names: Tasks can be created without a name (null=True, blank=True). Your application logic should handle cases where nombre_tarea is None.

Build docs developers (and LLMs) love