mirror of
https://github.com/jazzband/django-admin2.git
synced 2026-03-16 22:20:24 +00:00
Merge pull request #362 from powersurge360/modeladmin-ordering
Modeladmin ordering
This commit is contained in:
commit
3ddaaefba4
5 changed files with 51 additions and 5 deletions
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ class PostAdmin(djadmin2.ModelAdmin2):
|
|||
}
|
||||
save_on_top = True
|
||||
date_hierarchy = "published_date"
|
||||
ordering = ["-published_date", "title",]
|
||||
|
||||
|
||||
class CommentAdmin(djadmin2.ModelAdmin2):
|
||||
|
|
|
|||
|
|
@ -99,7 +99,16 @@ class PostListTest(BaseIntegrationTest):
|
|||
day=20,
|
||||
year=2012,
|
||||
)
|
||||
)
|
||||
),
|
||||
Post(
|
||||
title="post_5_title",
|
||||
body="body",
|
||||
published_date=datetime(
|
||||
month=6,
|
||||
day=20,
|
||||
year=2012,
|
||||
)
|
||||
),
|
||||
])
|
||||
|
||||
def test_view_ok(self):
|
||||
|
|
@ -182,7 +191,7 @@ class PostListTest(BaseIntegrationTest):
|
|||
|
||||
response = self.client.get(reverse('admin2:blog_post_index'))
|
||||
self.assertContains(response, '<a href="?year=2012">2012</a>')
|
||||
self.assertContains(response, "<tr>", 4)
|
||||
self.assertContains(response, "<tr>", 5)
|
||||
|
||||
response = self.client.get(
|
||||
"%s?%s" % (
|
||||
|
|
@ -199,7 +208,7 @@ class PostListTest(BaseIntegrationTest):
|
|||
response,
|
||||
'All dates',
|
||||
)
|
||||
self.assertContains(response, "<tr>", 3)
|
||||
self.assertContains(response, "<tr>", 4)
|
||||
|
||||
response = self.client.get(
|
||||
"%s?%s" % (
|
||||
|
|
@ -236,6 +245,30 @@ class PostListTest(BaseIntegrationTest):
|
|||
'May 2012'
|
||||
)
|
||||
|
||||
def test_ordering(self):
|
||||
self._create_posts()
|
||||
|
||||
response = self.client.get(reverse("admin2:blog_post_index"))
|
||||
|
||||
model_admin = response.context["view"].model_admin
|
||||
response_queryset = response.context["object_list"]
|
||||
manual_queryset = Post.objects.order_by("-published_date", "title")
|
||||
|
||||
zipped_queryset = zip(
|
||||
list(response_queryset),
|
||||
list(manual_queryset),
|
||||
)
|
||||
|
||||
self.assertTrue(all([
|
||||
model1.pk == model2.pk
|
||||
for model1, model2 in zipped_queryset
|
||||
]))
|
||||
|
||||
self.assertEqual(
|
||||
model_admin.get_ordering(response.request),
|
||||
model_admin.ordering,
|
||||
)
|
||||
|
||||
|
||||
class PostListTestCustomAction(BaseIntegrationTest):
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue