This commit is contained in:
Zanthras 2016-07-19 19:43:56 +00:00 committed by GitHub
commit ce82beee34
2 changed files with 9 additions and 0 deletions

View File

@ -66,6 +66,7 @@ class SiteListView(ObjectListView):
queryset = Site.objects.all() queryset = Site.objects.all()
filter = filters.SiteFilter filter = filters.SiteFilter
table = tables.SiteTable table = tables.SiteTable
sorting_attribute = 'name'
template_name = 'dcim/site_list.html' template_name = 'dcim/site_list.html'

View File

@ -1,3 +1,6 @@
from natsort import natsorted
from operator import attrgetter
from django_tables2 import RequestConfig from django_tables2 import RequestConfig
from django.contrib import messages from django.contrib import messages
@ -25,6 +28,7 @@ class ObjectListView(View):
filter = None filter = None
filter_form = None filter_form = None
table = None table = None
sorting_attribute = None
edit_permissions = [] edit_permissions = []
template_name = None template_name = None
redirect_on_single_result = True redirect_on_single_result = True
@ -68,6 +72,10 @@ class ObjectListView(View):
# Provide a hook to tweak the queryset based on the request immediately prior to rendering the object list # Provide a hook to tweak the queryset based on the request immediately prior to rendering the object list
self.queryset = self.alter_queryset(request) self.queryset = self.alter_queryset(request)
# If the sorting attribute is set, sort the final result based on the provided attribute key
if self.sorting_attribute:
self.queryset = natsorted(self.queryset, key=attrgetter(self.sorting_attribute))
# Construct the table based on the user's permissions # Construct the table based on the user's permissions
table = self.table(self.queryset) table = self.table(self.queryset)
table.model = model table.model = model