Skip to content

@robgolding63 /filtering.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Django Class-Based View Mixins: Part 2
class FilterMixin(object):
"""
View mixin which provides filtering for ListView.
"""
filter_url_kwarg = 'filter'
default_filter_param = None
def get_default_filter_param(self):
if self.default_filter_param is None:
raise ImproperlyConfigured(
"'FilterMixin' requires the 'default_filter_param' attribute "
"to be set.")
return self.default_filter_param
def filter_queryset(self, qs, filter_param):
"""
Filter the queryset `qs`, given the selected `filter_param`. Default
implementation does no filtering at all.
"""
return qs
def get_filter_param(self):
return self.kwargs.get(self.filter_url_kwarg,
self.get_default_filter_param())
def get_queryset(self):
return self.filter_queryset(
super(FilterMixin, self).get_queryset(),
self.get_filter_param())
def get_context_data(self, *args, **kwargs):
context = super(FilterMixin, self).get_context_data(*args, **kwargs)
context.update({
'filter': self.get_filter_param(),
})
return context
class SortMixin(object):
"""
View mixin which provides sorting for ListView.
"""
default_sort_params = None
def sort_queryset(self, qs, sort_by, order):
return qs
def get_default_sort_params(self):
if self.default_sort_params is None:
raise ImproperlyConfigured(
"'SortMixin' requires the 'default_sort_params' attribute "
"to be set.")
return self.default_sort_params
def get_sort_params(self):
default_sort_by, default_order = self.get_default_sort_params()
sort_by = self.request.GET.get('sort_by', default_sort_by)
order = self.request.GET.get('order', default_order)
return (sort_by, order)
def get_queryset(self):
return self.sort_queryset(
super(SortMixin, self).get_queryset(),
*self.get_sort_params())
def get_context_data(self, *args, **kwargs):
context = super(SortMixin, self).get_context_data(*args, **kwargs)
sort_by, order = self.get_sort_params()
context.update({
'sort_by': sort_by,
'order': order,
})
return context
@emilian

Thank you for these snippets..they help with filtering objects. Do you have any ideas on going beyond filtering to updating the filtered objects? For example, I would like to have a ListView where you can select a bunch of objects using checkboxes and then select actions from a drop down such as 'delete' or 'approve' (similar to the django admin actions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.