Django Generic Filter API¶
FilteredListView API¶
FilteredListView is a generic ListView allowing to filter queryset. It supports RawQuerySet, Python Object List as well as normal queryset.
It use a form to define which filters you want and some options to control the way querysets must be managed.
Example¶
First we define a form to handle our filters
from django import forms
from django_genericfilters.forms import FilteredForm
class TicketListForm(FilteredForm):
query = forms.CharField(label=_('Query'), required=False)
priority = forms.ChoiceField(label=_('Priority'),
required=False,
choices=(
('0', 'High'),
('1', 'Normal'),
('2', 'Low')
)
This form handle filter on a raw query and priority.
from django_genericfilters.views import FilteredListView
from myapp.models import Ticket
class TicketListView(FilteredListView):
model = Ticket
paginate_by = 10
form_class = TicketListForm
search_fields = ['creator__last_name', 'creator__first_name',
'subject', 'body']
filter_fields = ['status', 'priority']
qs_filter_fields = {'category__last_name': 'category',
'status': 'status',
'priority': 'priority'
}
default_order = 'last_updated_at'
FilteredListView Options¶
TicketListView define a set of useful specific options:
search_fields¶
a list of fields to search against with the “query” field defined on the form (see above)
filter_fields¶
a list of fields used to construct the form filters in the template. In this example, we define status and priority. By default, if we do not define a qs_filter_fields it will be used to filter the resulting queryset as well.
qs_filter_fields¶
a dict used to filter the results queryset. Useful if you need to make complex filters or related models filters.
qs_filter_fields_conditions¶
A dict used to filter the results queryset. Useful to add extra condition for a special field from qs_filter_fields.
default_order¶
the order you want your results to be sorted by default.