Skip to content

Instantly share code, notes, and snippets.

@robgolding
Created November 17, 2012 16:44
Show Gist options
  • Save robgolding/4097500 to your computer and use it in GitHub Desktop.
Save robgolding/4097500 to your computer and use it in GitHub Desktop.
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
Copy link

emilian commented Mar 26, 2013

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).

@vigo
Copy link

vigo commented Aug 31, 2017

You need to mix ListView, UpdateView and DeleteView all together. MRO is important

class MyMegaMixView(UpdateView, DeleteView, ListView):
    pass

and you need to impletment basics required methods of those classes. Normally ListView has no form but UpdateView has... thing that way...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment