django-easyfilters / docs / filterset.rst


This is meant to be used by subclassing. The only required attribute is fields, which must be a list of fields to produce filters for. For example, given the following model definition:

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    genre = models.ForeignKey(Genre)
    date_published = models.DateField() could create a BookFilterSet like this:

class BookFilterSet(FilterSet):
    fields = [

Each item in the fields attribute can also be a two-tuple containing first the field name and second a dictionary of options to be passed to the :doc:`filters <filters>` as keyword arguments, or a three-tuple containing the field name, a dictionary of options, and a Filter class. In this way you can override default options and the default filter type used e.g.:

from django_easyfilters.filters import ValuesFilter

class BookFilterSet(FilterSet):
    fields = [
        ('genre', dict(order_by_count=True)),
        ('date_published', {}, ValuesFilter),

This also allows :ref:`custom Filter classes <custom-filter-classes>` to be used.

To use the BookFilterSet, please see :doc:`the overview instructions <overview>`. The public API of FilterSet for use consists of:

In addition, there are methods/attributes that can be overridden to customise the FilterSet: