Django-filter Filtering Queryset Dynamically

  • Post author:
  • Post comments:0 Comments
  • Reading time:48 mins read

Django-filter is a powerful Python library for creating advanced filtering options for Django models. This library provides a simple and easy-to-use interface for creating custom filters that can be applied to querysets to retrieve specific data.

One of the main uses of django-filter is to provide a way for users to filter querysets in a web-based interface. With django-filter, developers can create custom filters that can be applied to querysets to retrieve specific data. The filters can be created using a variety of fields, including CharField, BooleanField, and DateField.

Here is an example of how to use the Django-filter library in a Django project.

First, you need to install the library by running “pip install django-filter” in your command line.

Then, in your file, define the model that you want to filter. For example

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    publication_date = models.DateField()
    price = models.DecimalField(max_digits=5, decimal_places=2)

Next, create a file and name, and in your file create a filter class that defines the fields that you want to use for filtering. For example.

import django_filters

class BookFilter(django_filters.FilterSet):
    title = django_filters.CharFilter(lookup_expr='icontains')
    author = django_filters.CharFilter(lookup_expr='icontains')
    publication_date = django_filters.DateFilter()
    price = django_filters.NumberFilter()
    class Meta:
        model = Book
        fields = ['title', 'author', 'publication_date', 'price']

Then open the file and in your file, import the filter class and use it to retrieve the filtered queryset.

from django.shortcuts import render
from .models import Book
from .filters import BookFilter

def book_list(request):
    book_list = Book.objects.all()
    book_filter = BookFilter(request.GET, queryset=book_list)
    return render(request, 'book_list.html', {'filter': book_filter})

Finally, create a HTML template name book_list.html and in your book_list.html a template file, use the filter to render the filtered queryset, and provide a form that allows users to apply filters

<form method="get">
  {{ filter.form.as_p }}
  <input type="submit" value="Filter">

      <th>Publication Date</th>
    {% for book in filter.qs %}
      <td>{{ book.title }}</td>
      <td>{{ }}</td>
      <td>{{ book.publication_date }}</td>
      <td>{{ book.price }}</td>
    {% endfor %}

This is a simple example of how to use Django-filter to filter querysets in a Django project. You can customize the filter class to suit your needs, such as adding more fields, or using different lookup expressions.

In addition to its powerful filtering capabilities, django-filter also supports pagination and ordering. This allows you to easily retrieve a specific page of data and order the results in a specific way.

Overall, django-filter is a powerful and easy-to-use Python library that can greatly simplify the process of creating advanced filtering options for Django models. Whether you’re a beginner or an experienced developer, django-filter is a valuable tool that can help you retrieve the specific data you need from your models.

Latest posts by Publisher (see all)


Publisher @ideasorblogs

Leave a Reply