Introduction

In this lesson, we will enhance our To-Do API by adding filtering capabilities. Filtering is vital for any API as it allows users to retrieve specific subsets of data based on criteria like completion status or priority. Imagine you have a large list of tasks and you want to see only the completed ones or those with a specific priority. That's where filtering comes into play.

Let's dive into the process of incorporating filtering into our Django application, making our To-Do API more powerful and user-friendly.

Recap of Previous Setup: Part 1

Before we jump into adding filtering, let's briefly recap the essential components of our project setup. This will help you follow along better. First of all, we have a model and a serializer for it. This time, we will use a bit extended version of our Todo model which includes the priority field.

# project/myapp/models.py
from django.db import models

class Todo(models.Model):
    task = models.CharField(max_length=255)
    completed = models.BooleanField(default=False)
    priority = models.IntegerField()

    def __str__(self):
        return self.task
from rest_framework import serializers
from .models import Todo

class TodoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Todo
        fields = '__all__'
Recap of Previous Setup: Part 2

Next, we have a set of implemented CRUD operations with Django generics:

from rest_framework import generics
from .models import Todo
from .serializers import TodoSerializer

class TodoListCreate(generics.ListCreateAPIView):
    queryset = Todo.objects.all()
    serializer_class = TodoSerializer

class TodoDetail(generics.RetrieveAPIView):
    queryset = Todo.objects.all()
    serializer_class = TodoSerializer

class TodoUpdate(generics.UpdateAPIView):
    queryset = Todo.objects.all()
    serializer_class = TodoSerializer

class TodoDelete(generics.DestroyAPIView):
    queryset = Todo.objects.all()
    serializer_class = TodoSerializer

This code outlines the core components we built for our basic To-Do API. Next, we'll enhance it with filtering capabilities.

Adding Filtering to the Todo API: Setup

To add filtering to our Todo API, we'll follow these steps:

  1. Install Django Filter: First, we need to install the django-filter library. If you're working on CodeSignal, this library is pre-installed, but for your local setup, you can install it using:

    pip install django-filter
  2. Update Project Settings: Now, let's update our Django settings to enable filtering:

    # project/settings.py
    INSTALLED_APPS = [
        ...
        'django_filters',
    ]
    
    REST_FRAMEWORK = {
        'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
    }

    This tells Django to use the DjangoFilterBackend for filtering. In most of the tasks, you will have this pre-defined.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal