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 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.
Release
To prepare a new version:
Create a branch named release/<version>
In a commit, change the CHANGELOG and VERSION file to remove the .dev0 and set the date of the release
In a second commit, change the VERSION to the next version number + .dev0
Create a PR for your branch
When the PR is merged, tag the first commit with the version number, and create a github release using the CHANGELOG
To release a new version (including the wheel):
pip install twine python setup.py sdist bdist_wheel twine upload --repository-url https://test.pypi.org/legacy/ dist/*
And after testing everything works fine on the testing repository:
twine upload dist/*
Ressources
Documentation: https://django-generic-filters.readthedocs.io
PyPI page: http://pypi.python.org/pypi/django-generic-filters
Code repository: https://github.com/novapost/django-generic-filters
Bugtracker: https://github.com/novapost/django-generic-filters/issues
Continuous integration: https://travis-ci.org/novapost/django-generic-filters
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-generic-filters-2.0.0.tar.gz.
File metadata
- Download URL: django-generic-filters-2.0.0.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba1a8a27458b668dfbb3e7075fcf23322a94d74f8cc455beaad724eeb63f357a
|
|
| MD5 |
f27a67300bc00cb658e76e5f24645f7b
|
|
| BLAKE2b-256 |
9d8b65d4b0ccdd33d847bf069eb9abe5dad635893f14fac55c47921ac87bcecc
|
File details
Details for the file django_generic_filters-2.0.0-py3-none-any.whl.
File metadata
- Download URL: django_generic_filters-2.0.0-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec767034090b97bc012627cca03fa59ae1763c91b7dbd0a71dba3741067d9569
|
|
| MD5 |
fef4fa327c5a8ec44fb06f1dfd2ceeee
|
|
| BLAKE2b-256 |
6ed3a9182e1aaab9a560ee4ddb8bc7ecb71fc97870b60c4c110be8157764e101
|