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.
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 gettext_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
Documentation: https://django-generic-filters.readthedocs.io
PyPI page: http://pypi.python.org/pypi/django-generic-filters
Code repository: https://github.com/peopledoc/django-generic-filters
Bugtracker: https://github.com/peopledoc/django-generic-filters/issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-generic-filters-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a314a336a37e9db2b51025aadc92aafb8032eba49ef66c885f7415a071852f6e |
|
MD5 | 61e01f2858758094d182f392eb46c4d0 |
|
BLAKE2b-256 | 09af77eef8893e3c4cc13751f47d81ad5c19ff04e93098099f56682ef6379970 |
Hashes for django_generic_filters-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71de1f3a53a1dae0a59cd41f6a0bd2d0a681e747defa8e365d8bf463a2adb039 |
|
MD5 | f63f3ef97797e2179c70a329ce4efd7f |
|
BLAKE2b-256 | 9a39b703c2b0633c3759f614c1603b0c082f50cc454cff5b64d21ab22a2109d4 |