Skip to main content
https://secure.travis-ci.org/freevoid/django-datafilters.png?branch=master

Abstract

Library to implement reusable queryset filtering for django-powered websites.

This library provides a way to declaratively define filter specifications in a django-forms manner. Such forms can be used as ordinary django forms and they also provides a method filter to perform filtering of arbitrary querysets.

This approach is somewhat differs from one in django-admin, but it looks more intuitive and straightforward for the author.

Main features:

  • forms-based declaration and usage;

  • simple API;

  • easy to implement and reuse abstract filter specs of any complexity (from standard lookups to QuerySet.extra calls);

  • runtime-aware filtering;

  • a number of builtin specs for simple cases.

Usage

For concrete usage example, see sample_proj/polls/views.py.

To perform filtering one must define a subclass of datafilters.filterform.FilterForm (base class for filter forms). The typical declaration consists of class attributes declaring filter specifications, subclasses of datafilters.filterspec.FilterSpec. This tandem is very much like the Form and Field pair. FilterSpec subclass defines a corresponding form field that will be used to render and validate a django form and it also defines a method to get lookup conditions based on user input (to_lookup). There is a bunch of builtin specs so typicaly it is not necessary to implement your own filter specs for simple filtering.

For example purposes we will use models from django tutorial: Choice and Question.

The typical filter form looks like that:

from datafilters.filterform import FilterForm
from datafilters.specs import ContainsFilterSpec, \
    SelectBoolFilterSpec

class ChoicesFilterForm(FilterForm):
    choice_text = ContainsFilterSpec('choice',
                                     label='Choice contains text')
    question_text = ContainsFilterSpec('poll__question',
                                       label='Question contains text')
    has_votes = GreaterThanZeroFilterSpec('votes')

Direct usage of filter form

Defined form can be used directly:

from django.shortcuts import render_to_response
from polls.models import Choice

def choice_list(request):
    choices = Choice.objects.all()
    filterform = ChoicesFilterForm(request.GET)
    if filterform.is_valid():
        choices = filterform.filter(choices)

    return render_to_response('polls/choice_list.html',
        {
            'choices': choices,
            'filterform': filterform,
        })

filter_powered decorator

There is a decorator to remove bottlenecks when using filtering extensively:

from django.template.response import TemplateResponse
from datafilters.decorators import filter_powered

@filter_powered(ChoicesFilterForm, queryset_name='choices')
def choice_list(request):
    choices = Choice.objects.all()
    return TemplateResponse('polls/choice_list.html',
        {'choices': choices})

View mixin

If you are using django class-based views there is another option to take: a view mixin FilterFormMixin. Example:

from django.views.generic import ListView

class ChoiceListView(FilterFormMixin, ListView):
    model = Choice
    filter_form_cls = ChoicesFilterForm

choice_list = ChoiceListView.as_view()

Usage in templates

In our template we can use new context variable filterform as an ordinary django form:

<form class="filter" method="get" action="">
    {{ filterform.as_p }}
    <input type="submit" value="Apply filter" />
</form>

Requirements

Metadata

Release files for datafilters 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datafilters 0.3.3
File Size Uploaded Provenance Actions
datafilters-0.3.3.zip 25.0 kB Without provenance Details for datafilters-0.3.3.zip

Built distribution (wheel)

Table of built distributions (wheels) for datafilters 0.3.3
File Interpreter ABI Platform Actions
datafilters-0.3.3.win-amd64.exe Details for datafilters-0.3.3.win-amd64.exe

Total release size: 271.1 kB

Release files / datafilters-0.3.3.zip

Download URL datafilters-0.3.3.zip
Size 25.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b51b7d14069a0d8b4ece67e663612d3e19ecdbf00858bb6e8196bebf05e161d8
BLAKE2b-256 checksum
How to use checksums
0cfe8e1b6ba798d7e0b7776d6e6c447b8f4c4f42c5737a11c70bfde7676f8cd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / datafilters-0.3.3.win-amd64.exe

Download URL datafilters-0.3.3.win-amd64.exe
Size 246.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a50ffd7fe03cba099050897d9248c83a135e7a5a2f365e13463b1e67ebb6f351
BLAKE2b-256 checksum
How to use checksums
f579be1ca710f78b6b1c01237d7996b08c16caaa541ef2fc036c84480edb8763
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for datafilters 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page