django-easyfilters / docs / filterset.rst

FilterSet

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()

...you could create a BookFilterSet like this:

class BookFilterSet(FilterSet):
    fields = [
        'genre',
        'authors',
        'date_published',
    ]

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:

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.