Skip to content

Instantly share code, notes, and snippets.

@craigderington craigderington/views.py
Last active Apr 1, 2016

Embed
What would you like to do?
Generic Customer List View - Search & Filter QuerySet
from django.views.generic import ListView
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.query_utils import Q
from django_tables2 import RequestConfig
from braces.views import LoginRequiredMixin, GroupRequiredMixin
from .tables import CustomerTable
from .filters import CustomerListFilter
from .utils import PagedFilteredTableView
from .models import Customer
from .forms import CustomerListFormHelper
class CustomerListView(LoginRequiredMixin, GroupRequiredMixin, PagedFilteredTableView):
model = Customer
template_name = 'customer_list.html'
context_object_name = 'customer'
ordering = ['-id']
group_required = u'company-user'
table_class = CustomerTable
filter_class = CustomerListFilter
formhelper_class = CustomerListFormHelper
def get_queryset(self):
qs = super(CustomerListView, self).get_queryset()
return qs
def post(self, request, *args, **kwargs):
return PagedFilteredTableView.as_view()(request)
def get_context_data(self, **kwargs):
context = super(CustomerListView, self).get_context_data(**kwargs)
context['nav_customer'] = True
search_query = self.get_queryset()
table = CustomerTable(search_query)
RequestConfig(self.request, paginate={'per_page': 30}).configure(table)
context['table'] = table
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.