Implemented ordering, tests in the next commit.

This commit is contained in:
Kevin Diale 2013-08-01 12:00:03 -04:00
parent 361aeda9da
commit 1aa4ee36d6
4 changed files with 15 additions and 2 deletions

View file

@ -12,7 +12,7 @@ MODEL_ADMIN_ATTRS = (
'list_display_links', 'list_filter', 'admin', 'search_fields',
'field_renderers', 'index_view', 'detail_view', 'create_view',
'update_view', 'delete_view', 'get_default_view_kwargs',
'get_list_actions', 'actions_on_bottom', 'actions_on_top',
'save_on_top', 'save_on_bottom', 'readonly_fields', )
'get_list_actions', 'get_ordering', 'actions_on_bottom', 'actions_on_top',
'ordering', 'save_on_top', 'save_on_bottom', 'readonly_fields', )
ADMIN2_THEME_DIRECTORY = getattr(settings, "ADMIN2_THEME_DIRECTORY", "djadmin2theme_default")

View file

@ -73,6 +73,7 @@ class ModelAdmin2(with_metaclass(ModelAdminBase2)):
verbose_name = None
verbose_name_plural = None
model_admin_attributes = settings.MODEL_ADMIN_ATTRS
ordering = False
save_on_top = False
save_on_bottom = True
@ -261,6 +262,9 @@ class ModelAdmin2(with_metaclass(ModelAdminBase2)):
}
return actions_dict
def get_ordering(self, request):
return self.ordering
class Admin2Inline(extra_views.InlineFormSet):
"""

View file

@ -153,6 +153,8 @@ class ModelListView(AdminModel2Mixin, generic.ListView):
queryset, search_use_distinct = self.get_search_results(
queryset, search_term)
queryset = self._modify_queryset_for_ordering(queryset)
if self.model_admin.list_filter:
queryset = self.build_list_filter(queryset).qs
@ -166,6 +168,12 @@ class ModelListView(AdminModel2Mixin, generic.ListView):
else:
return queryset
def _modify_queryset_for_ordering(self, queryset):
ordering = self.model_admin.get_ordering(self.request)
if ordering:
queryset = queryset.order_by(*ordering)
return queryset
def _modify_queryset_for_sort(self, queryset):
# If we are sorting AND the field exists on the model
sort_by = self.request.GET.get('sort', None)

View file

@ -35,6 +35,7 @@ class PostAdmin(djadmin2.ModelAdmin2):
}
save_on_top = True
date_hierarchy = "published_date"
ordering = ["published_date", "title",]
class CommentAdmin(djadmin2.ModelAdmin2):