📜  django-filter - Python (1)

📅  最后修改于: 2023-12-03 15:14:44.713000             🧑  作者: Mango

Django-filter - Python

Django-filter is a package for Django framework that provides easy to use filtering for QuerySets. It allows users to easily filter down a queryset based on a model’s fields, displaying the form to allow for this filtering and handling the server-side processing of the query parameters. In simple words, it provides an easy way to filter and sort the objects in our Django application.

Here are some of the features that Django-filter provides:

  • Provides a filter specification for the QuerySet API
  • Supports multiple parameterized filters
  • Supports multiple filter types: string, date, number, and UUID filters
  • Provides several filter methods: exact, contains, gt, lt, etc.
  • Provides a filter form that automatically generates a form for filters based on the model
  • Provides flexible control over the filter form
  • Supports custom filtering methods
  • Integrates well with Django's ModelForms and its built-in validation
Installation

Django-filter can be installed using pip. Open a terminal and run the following command:

pip install django-filter
Usage

To use django-filter, you just need to define a filter class that inherits from django_filters.FilterSet. This class should define the model and fields to filter.

Here is a simple example:

import django_filters
from .models import Book

class BookFilter(django_filters.FilterSet):
    title = django_filters.CharFilter(lookup_expr='icontains')

    class Meta:
        model = Book
        fields = ['title', 'author', 'publisher']

In the above code, we import the django_filters module and define a BookFilter class that inherits from django_filters.FilterSet. This class contains a filter for the title field of the Book model. Here, we are using the icontains filter, which performs a case-insensitive lookup for the given keyword in the title field.

We also define a Meta class that specifies the model and the fields to filter.

Now we need to create a view that uses this filter to filter the queryset. Here is how we can create a view to display the filtered results:

from django.shortcuts import render
from django_filters.views import FilterView
from .models import Book
from .filters import BookFilter

class BookListView(FilterView):
    model = Book
    filterset_class = BookFilter
    template_name = 'book_list.html'

In the above code, we import the FilterView class from django_filters.views. This view takes care of displaying the filter form and filtering the results based on the user input.

We define a BookListView class that inherits from FilterView. Here, we specify the model, filterset_class, and template_name properties. The filterset_class property is set to BookFilter, which we defined earlier.

Finally, we create a template to display the results. Here is an example:

{% extends 'base.html' %}

{% block content %}
    <h1>Books</h1>
    <form method="get" action=".">
        {{ filter.form.as_p }}
        <button type="submit">Filter</button>
    </form>
    <hr>
    <ul>
    {% for book in filter.qs %}
        <li>{{ book.title }} by {{ book.author }}</li>
    {% empty %}
        <li>No books found.</li>
    {% endfor %}
    </ul>
{% endblock %}

In the above code, we use the filter.form.as_p property to render the filter form. We also loop through the filtered queryset and display the book titles and authors.

Conclusion

Django-filter is a very useful package that provides easy-to-use filtering for Django applications. It simplifies the filtering process by providing an easy way to define filters and generating the filter form automatically. With its flexible control over the filter form and support for custom filtering methods, Django-filter makes it easy to implement complex filtering requirements.