How to Build a Task Manager API with Django REST Framework: Part 2

Introduction

Welcome back to our Django REST Framework (DRF) adventure! In Part 1, we laid the groundwork by setting up a Django project, installing DRF, and crafting a Task model. Now, in Part 2, we’re diving into the fun stuff—building API…


This content originally appeared on DEV Community and was authored by kihuni

Introduction

Welcome back to our Django REST Framework (DRF) adventure! In Part 1, we laid the groundwork by setting up a Django project, installing DRF, and crafting a Task model. Now, in Part 2, we’re diving into the fun stuff—building API endpoints to Create, Read, Update, and Delete (CRUD) tasks. By the end of this guide, your Task Manager API will be fully functional, and ready to test with tools like Postman or DRF’s browsable API.

Let’s make your API come alive, step by step!

Table of Contents

  • Step 1: Create a Serializer
  • Step 2: Build API Views
  • Step 3: Define URL Routes
  • Step 4: Test the API
  • Conclusion
  • What’s Next?

Step 1: Create a Serializer

Serializers are the bridge between your Django models and JSON data—perfect for REST APIs. Let’s create one for our Task model.

In your tasks app, create a new file called serializers.py and add:

from rest_framework import serializers
from .models import Task

class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = '__all__'

What’s happening here?

The TaskSerializer automatically converts Task model instances into JSON (and back again) using DRF’s ModelSerializer. The fields = '__all__' line includes all model fields—title, description, completed, and created_at—in the API responses.

Step 2: Build API Views

Time to define the views that power our CRUD operations! DRF’s generic views make this a breeze.

Open tasks/views.py and update it with:

from rest_framework import generics
from .models import Task
from .serializers import TaskSerializer

class TaskListCreateView(generics.ListCreateAPIView):
    queryset = Task.objects.all()
    serializer_class = TaskSerializer

class TaskDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Task.objects.all()
    serializer_class = TaskSerializer

Breaking it down:

  • TaskListCreateView: Handles GET (list all tasks) and POST (create a new task).
  • TaskDetailView: Manages GET (retrieve a task), PUT (update a task), and DELETE (delete a task) for a specific task ID.
  • DRF’s generic class-based views (CBVs) save us from writing boilerplate code—efficiency at its finest!

Step 3: Define URL Routes

Let’s wire up our views with URL routes so users can hit those endpoints.

Create or edit tasks/urls.py with:

from django.urls import path
from .views import TaskListCreateView, TaskDetailView

urlpatterns = [
    path('tasks/', TaskListCreateView.as_view(), name='task-list-create'),
    path('tasks/<int:pk>/', TaskDetailView.as_view(), name='task-detail'),
]

Then, connect it to your project’s main taskmanager/urls.py:

from django.urls import path, include

urlpatterns = [
    path('api/', include('tasks.urls')),
]

Your API endpoints are now live:

  • GET /api/tasks/ - Fetch all tasks
  • POST /api/tasks/ - Add a new task
  • GET /api/tasks/{id}/ - View a specific task
  • PUT /api/tasks/{id}/ - Update a task
  • DELETE /api/tasks/{id}/ - Remove a task

Step 4: Test the API

Start the Django development server:

python manage.py runserver

Now, let’s test your shiny new API with these methods:

Django’s Browsable API

Visit http://127.0.0.1:8000/api/tasks/ in your browser. DRF’s built-in interface lets you:

Browsable API

browsable api

browsable api

Postman

Use Postman to send requests:

Postman Testing
postman

New to Postman? Check out this quick guide.

cURL
For command-line fans:

  • List tasks:
curl -X GET http://127.0.0.1:8000/api/tasks/

Curl

  • Create a new task:
curl -X POST -H "Content-Type: application/json" \
-d '{"title": "Second Series", "description": "Write Part 2 of Task Manager API", "completed": false}' \
http://127.0.0.1:8000/api/tasks/

Curl

Conclusion

🎉 Boom! You’ve just built a fully functional CRUD API with Django REST Framework. Your Task Manager API can now handle creating, reading, updating, and deleting tasks.

Summary Checklist

  • ✅ Created a serializer for the Task model
  • ✅ Built API views for CRUD operations
  • ✅ Defined URL routes for your endpoints
  • ✅ Tested the API with DRF’s browsable API, Postman, and cURL

What’s Next?

In Part 3, we’ll lock down our API with authentication to keep those tasks secure. Stay tuned for more 🚀

💬 Found this guide helpful? Let me know in the comments😊


This content originally appeared on DEV Community and was authored by kihuni


Print Share Comment Cite Upload Translate Updates
APA

kihuni | Sciencx (2025-03-05T10:11:07+00:00) How to Build a Task Manager API with Django REST Framework: Part 2. Retrieved from https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/

MLA
" » How to Build a Task Manager API with Django REST Framework: Part 2." kihuni | Sciencx - Wednesday March 5, 2025, https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/
HARVARD
kihuni | Sciencx Wednesday March 5, 2025 » How to Build a Task Manager API with Django REST Framework: Part 2., viewed ,<https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/>
VANCOUVER
kihuni | Sciencx - » How to Build a Task Manager API with Django REST Framework: Part 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/
CHICAGO
" » How to Build a Task Manager API with Django REST Framework: Part 2." kihuni | Sciencx - Accessed . https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/
IEEE
" » How to Build a Task Manager API with Django REST Framework: Part 2." kihuni | Sciencx [Online]. Available: https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/. [Accessed: ]
rf:citation
» How to Build a Task Manager API with Django REST Framework: Part 2 | kihuni | Sciencx | https://www.scien.cx/2025/03/05/how-to-build-a-task-manager-api-with-django-rest-framework-part-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.