0
0
Djangoframework~30 mins

Task results and status in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Task Results and Status in Django
📖 Scenario: You are building a simple Django app to track tasks and their results. Each task has a name and a status that shows if it is pending, completed, or failed.
🎯 Goal: Create a Django model to store tasks with their results and status. Then, configure a status choice field, write a query to filter completed tasks, and finally add a method to display the task status nicely.
📋 What You'll Learn
Create a Django model named Task with fields name (CharField) and status (CharField).
Define a STATUS_CHOICES tuple with values 'pending', 'completed', and 'failed'.
Write a Django ORM query to get all tasks with status 'completed'.
Add a method get_status_display in the Task model to return a user-friendly status string.
💡 Why This Matters
🌍 Real World
Tracking task status is common in project management, bug tracking, and workflow automation apps.
💼 Career
Understanding Django models, choices, and queries is essential for backend web development jobs using Django.
Progress0 / 4 steps
1
Create the Task model with name and status fields
Create a Django model called Task with a name field as models.CharField(max_length=100) and a status field as models.CharField(max_length=20).
Django
Need a hint?

Use models.CharField for both fields with the specified max_length values.

2
Add STATUS_CHOICES tuple and link it to the status field
Add a tuple called STATUS_CHOICES with these exact pairs: ('pending', 'Pending'), ('completed', 'Completed'), and ('failed', 'Failed'). Then update the status field to use choices=STATUS_CHOICES.
Django
Need a hint?

Define STATUS_CHOICES as a tuple of tuples and pass it to the choices argument of the status field.

3
Write a query to get all completed tasks
Write a Django ORM query called completed_tasks that filters the Task model for all tasks where status is exactly 'completed'.
Django
Need a hint?

Use Task.objects.filter(status='completed') to get all completed tasks.

4
Add a method to display the status nicely
Add a method called get_status_display inside the Task model that returns the display name of the current status using Django's built-in get_FOO_display() pattern.
Django
Need a hint?

Implement the method using dict(self.STATUS_CHOICES).get(self.status, self.status) to get the display value.