Skip to main content

Easy filters for your Generic ListView with Django.

Project description

django-generic-filters is a toolkit to filter results of Django’s ListView, using forms.

Main use cases are obviously search forms and filtered lists.

As a developer, given you have a ListView, in order to let the user filter the results:

  • use a form to easily render the filters as HTML;

  • the user typically sends the filters via GET;

  • validate the user’s input using a Django form;

  • filter the Django view’s queryset using form’s cleaned data.

Build Status

Example

views.py

from django_genericfilters.views import FilteredListView


class UserListView(FilteredListView):
    # ListView options. FilteredListView inherits from ListView.
    model = User
    template_name = 'user/user_list.html'
    paginate_by = 10
    context_object_name = 'users'

    # FormMixin options. FilteredListView inherits from FormMixin.
    form_class = UserListForm

    # FilteredListView options.
    search_fields = ['first_name', 'last_name', 'username', 'email']
    filter_fields = ['is_active', 'is_staff', 'is_superuser']
    default_order = 'last_name'

    def form_valid(self, form):
        """Return the queryset when form has been submitted."""
        queryset = super(UserListView, self).form_valid(form)

        # Handle specific fields of the custom ListForm
        # Others are automatically handled by FilteredListView.

        if form.cleaned_data['is_active'] == 'yes':
            queryset = queryset.filter(is_active=True)
        elif form.cleaned_data['is_active'] == 'no':
            queryset = queryset.filter(is_active=False)

        if form.cleaned_data['is_staff'] == 'yes':
            queryset = queryset.filter(is_staff=True)
        elif form.cleaned_data['is_staff'] == 'no':
            queryset = queryset.filter(is_staff=False)

        if form.cleaned_data['is_superuser'] == 'yes':
            queryset = queryset.filter(is_superuser=True)
        elif form.cleaned_data['is_superuser'] == 'no':
            queryset = queryset.filter(is_superuser=False)

        return queryset

forms.py

from django import forms
from django.utils.translation import ugettext_lazy as _
from django_genericfilters import forms as gf


class UserListForm(gf.QueryFormMixin, gf.OrderFormMixin, gf.FilteredForm):
    is_active = gf.ChoiceField(label=_('Status'),
                               choices=(('yes', _('Active')),
                                        ('no', _('Unactive'))))

    is_staff = gf.ChoiceField(label=_('Staff'))

    is_superuser = gf.ChoiceField(label=_('Superuser'))

    def get_order_by_choices(self):
        return [('date_joined', _(u'date joined')),
                ('last_login', _(u'last login')),
                ('last_name', _(u'Name'))]

Forms

Several form mixins are provided to cover frequent use cases:

  • OrderFormMixin with order_by and order_reverse fields.

  • QueryFormMixin for little full-text search using icontains.

See “mixin” documentation for details.

Ressources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-generic-filters-0.10.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_generic_filters-0.10-py2-none-any.whl (19.0 kB view details)

Uploaded Python 2

File details

Details for the file django-generic-filters-0.10.tar.gz.

File metadata

File hashes

Hashes for django-generic-filters-0.10.tar.gz
Algorithm Hash digest
SHA256 6bd2851439bb8fd008bc5da8d8f816fa39c55f1e1d8b279cd057b04917d2671e
MD5 2e27a84987474ba22c8a512f9f4b0af8
BLAKE2b-256 6df077c485f847513b62c2986f6e17d8d2c8b896d278e63fece1faf5493c3f4f

See more details on using hashes here.

File details

Details for the file django_generic_filters-0.10-py2-none-any.whl.

File metadata

File hashes

Hashes for django_generic_filters-0.10-py2-none-any.whl
Algorithm Hash digest
SHA256 3a4d07c6c4a0f5ccdb9848ddf05e12ec492cb1904796a3411d083d2619f89a94
MD5 71da2bf83aaf0a8a185731b3777a7bd6
BLAKE2b-256 cf91db9779fa293a37673de4509dcb178313de89b2f45e33fb5edd5c4e059894

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page