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
File details
Details for the file django-generic-filters-2.1.1.tar.gz
.
File metadata
- Download URL: django-generic-filters-2.1.1.tar.gz
- Upload date:
- Size: 37.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f2fb4d212a10bc9495c2089758e0eedec042763c8cca86fc2f6de3311f058ea |
|
MD5 | dee765a97661ea14283fe4d21bdf3a66 |
|
BLAKE2b-256 | c54a9c0df462f9e014dd8c38cb5519f9393d5cf0fa968fa5578e0720f827c0aa |
File details
Details for the file django_generic_filters-2.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_generic_filters-2.1.1-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed27828f1c1af0f0c6d0615b1df15c8c9cfe112f2cfd37c3c50a3b46f165f1d4 |
|
MD5 | cbe6e845ce635f83ae91ce5002754594 |
|
BLAKE2b-256 | 3d5526d8b05e3cadf91d86015391384b658613a73a4a1c09114baae8cd1fea6f |