Skip to content

Commit 7bba824

Browse files
DrMeerstimgraham
authored andcommitted
Fixed #28496 -- Added ModelAdmin.get_changelist_instance().
1 parent fea9cb4 commit 7bba824

File tree

3 files changed

+125
-152
lines changed

3 files changed

+125
-152
lines changed

django/contrib/admin/options.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,32 @@ def get_changelist(self, request, **kwargs):
659659
from django.contrib.admin.views.main import ChangeList
660660
return ChangeList
661661

662+
def get_changelist_instance(self, request):
663+
"""
664+
Return a `ChangeList` instance based on `request`. May raise
665+
`IncorrectLookupParameters`.
666+
"""
667+
list_display = self.get_list_display(request)
668+
list_display_links = self.get_list_display_links(request, list_display)
669+
# Add the action checkboxes if any actions are available.
670+
if self.get_actions(request):
671+
list_display = ['action_checkbox'] + list(list_display)
672+
ChangeList = self.get_changelist(request)
673+
return ChangeList(
674+
request,
675+
self.model,
676+
list_display,
677+
list_display_links,
678+
self.get_list_filter(request),
679+
self.date_hierarchy,
680+
self.get_search_fields(request),
681+
self.get_list_select_related(request),
682+
self.list_per_page,
683+
self.list_max_show_all,
684+
self.list_editable,
685+
self,
686+
)
687+
662688
def get_object(self, request, object_id, from_field=None):
663689
"""
664690
Return an instance matching the field and value provided, the primary
@@ -1522,26 +1548,8 @@ def changelist_view(self, request, extra_context=None):
15221548
if not self.has_change_permission(request, None):
15231549
raise PermissionDenied
15241550

1525-
list_display = self.get_list_display(request)
1526-
list_display_links = self.get_list_display_links(request, list_display)
1527-
list_filter = self.get_list_filter(request)
1528-
search_fields = self.get_search_fields(request)
1529-
list_select_related = self.get_list_select_related(request)
1530-
1531-
# Check actions to see if any are available on this changelist
1532-
actions = self.get_actions(request)
1533-
if actions:
1534-
# Add the action checkboxes if there are any actions available.
1535-
list_display = ['action_checkbox'] + list(list_display)
1536-
1537-
ChangeList = self.get_changelist(request)
15381551
try:
1539-
cl = ChangeList(
1540-
request, self.model, list_display,
1541-
list_display_links, list_filter, self.date_hierarchy,
1542-
search_fields, list_select_related, self.list_per_page,
1543-
self.list_max_show_all, self.list_editable, self,
1544-
)
1552+
cl = self.get_changelist_instance(request)
15451553
except IncorrectLookupParameters:
15461554
# Wacky lookup parameters were given, so redirect to the main
15471555
# changelist page, without parameters, and pass an 'invalid=1'
@@ -1562,6 +1570,7 @@ def changelist_view(self, request, extra_context=None):
15621570
action_failed = False
15631571
selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
15641572

1573+
actions = self.get_actions(request)
15651574
# Actions with no confirmation
15661575
if (actions and request.method == 'POST' and
15671576
'index' in request.POST and '_save' not in request.POST):

0 commit comments

Comments
 (0)