Consider a Django view that checks a task's status and returns a message accordingly.
from django.http import JsonResponse
from django.views import View
class TaskStatusView(View):
def get(self, request, task_id):
task = get_task_by_id(task_id) # Returns a dict with 'status'
if task['status'] == 'completed':
return JsonResponse({'result': 'Task finished successfully'})
else:
return JsonResponse({'result': 'Task still running'})
What will be the JSON response if task['status'] is 'completed'?
from django.http import JsonResponse from django.views import View class TaskStatusView(View): def get(self, request, task_id): task = {'status': 'completed'} if task['status'] == 'completed': return JsonResponse({'result': 'Task finished successfully'}) else: return JsonResponse({'result': 'Task still running'})
Check the condition that matches the task status.
The view returns a JSON response with {'result': 'Task finished successfully'} when the task status is 'completed'.
task_status after running this Django signal handler?Given this Django signal handler that updates a task's status after completion:
from django.dispatch import receiver
from django.db.models.signals import post_save
@receiver(post_save, sender=Task)
def update_task_status(sender, instance, **kwargs):
if instance.is_finished:
instance.status = 'completed'
instance.save()
# Assume a Task instance with is_finished=True and initial status='pending'What will be the status of the task after the signal runs?
class Task: def __init__(self, is_finished, status): self.is_finished = is_finished self.status = status def save(self): pass task = Task(is_finished=True, status='pending') if task.is_finished: task.status = 'completed' task.save() task_status = task.status
Check what happens when is_finished is True.
The signal sets the status to 'completed' if is_finished is True, so the final status is 'completed'.
You want to define a Django model field status that only allows 'pending', 'running', or 'completed'. Which code snippet is correct?
Choices must be a list of tuples pairing database value and human-readable name.
Option B correctly uses a list of tuples for choices. Option B uses a dict which is invalid. Option B uses a tuple of strings without pairs. Option B uses a list of strings without tuples.
Given this async function to check a task status:
async def check_task_status(task_id):
task = Task.objects.get(id=task_id)
if task.status == 'completed':
return 'Done'
else:
return 'In progress'
When called in an async context, it raises an error. Why?
Consider how Django ORM works with async code.
Django ORM methods like get() are synchronous and block the event loop if called in async functions without special async ORM support.
You want to run long tasks in the background and check their status and results later in Django. Which feature is best suited for this?
Think about background task queues and result storage.
Celery integrates with Django to run tasks asynchronously and track their status and results efficiently.