Fixed #28496 -- Added ModelAdmin.get_changelist_instance().
This commit is contained in:
parent
fea9cb46aa
commit
7bba82453c
|
@ -659,6 +659,32 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
from django.contrib.admin.views.main import ChangeList
|
from django.contrib.admin.views.main import ChangeList
|
||||||
return ChangeList
|
return ChangeList
|
||||||
|
|
||||||
|
def get_changelist_instance(self, request):
|
||||||
|
"""
|
||||||
|
Return a `ChangeList` instance based on `request`. May raise
|
||||||
|
`IncorrectLookupParameters`.
|
||||||
|
"""
|
||||||
|
list_display = self.get_list_display(request)
|
||||||
|
list_display_links = self.get_list_display_links(request, list_display)
|
||||||
|
# Add the action checkboxes if any actions are available.
|
||||||
|
if self.get_actions(request):
|
||||||
|
list_display = ['action_checkbox'] + list(list_display)
|
||||||
|
ChangeList = self.get_changelist(request)
|
||||||
|
return ChangeList(
|
||||||
|
request,
|
||||||
|
self.model,
|
||||||
|
list_display,
|
||||||
|
list_display_links,
|
||||||
|
self.get_list_filter(request),
|
||||||
|
self.date_hierarchy,
|
||||||
|
self.get_search_fields(request),
|
||||||
|
self.get_list_select_related(request),
|
||||||
|
self.list_per_page,
|
||||||
|
self.list_max_show_all,
|
||||||
|
self.list_editable,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
|
||||||
def get_object(self, request, object_id, from_field=None):
|
def get_object(self, request, object_id, from_field=None):
|
||||||
"""
|
"""
|
||||||
Return an instance matching the field and value provided, the primary
|
Return an instance matching the field and value provided, the primary
|
||||||
|
@ -1522,26 +1548,8 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
if not self.has_change_permission(request, None):
|
if not self.has_change_permission(request, None):
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
|
|
||||||
list_display = self.get_list_display(request)
|
|
||||||
list_display_links = self.get_list_display_links(request, list_display)
|
|
||||||
list_filter = self.get_list_filter(request)
|
|
||||||
search_fields = self.get_search_fields(request)
|
|
||||||
list_select_related = self.get_list_select_related(request)
|
|
||||||
|
|
||||||
# Check actions to see if any are available on this changelist
|
|
||||||
actions = self.get_actions(request)
|
|
||||||
if actions:
|
|
||||||
# Add the action checkboxes if there are any actions available.
|
|
||||||
list_display = ['action_checkbox'] + list(list_display)
|
|
||||||
|
|
||||||
ChangeList = self.get_changelist(request)
|
|
||||||
try:
|
try:
|
||||||
cl = ChangeList(
|
cl = self.get_changelist_instance(request)
|
||||||
request, self.model, list_display,
|
|
||||||
list_display_links, list_filter, self.date_hierarchy,
|
|
||||||
search_fields, list_select_related, self.list_per_page,
|
|
||||||
self.list_max_show_all, self.list_editable, self,
|
|
||||||
)
|
|
||||||
except IncorrectLookupParameters:
|
except IncorrectLookupParameters:
|
||||||
# Wacky lookup parameters were given, so redirect to the main
|
# Wacky lookup parameters were given, so redirect to the main
|
||||||
# changelist page, without parameters, and pass an 'invalid=1'
|
# changelist page, without parameters, and pass an 'invalid=1'
|
||||||
|
@ -1562,6 +1570,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
action_failed = False
|
action_failed = False
|
||||||
selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
|
selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
|
||||||
|
|
||||||
|
actions = self.get_actions(request)
|
||||||
# Actions with no confirmation
|
# Actions with no confirmation
|
||||||
if (actions and request.method == 'POST' and
|
if (actions and request.method == 'POST' and
|
||||||
'index' in request.POST and '_save' not in request.POST):
|
'index' in request.POST and '_save' not in request.POST):
|
||||||
|
|
|
@ -5,7 +5,7 @@ from django.contrib.admin.models import LogEntry
|
||||||
from django.contrib.admin.options import IncorrectLookupParameters
|
from django.contrib.admin.options import IncorrectLookupParameters
|
||||||
from django.contrib.admin.templatetags.admin_list import pagination
|
from django.contrib.admin.templatetags.admin_list import pagination
|
||||||
from django.contrib.admin.tests import AdminSeleniumTestCase
|
from django.contrib.admin.tests import AdminSeleniumTestCase
|
||||||
from django.contrib.admin.views.main import ALL_VAR, SEARCH_VAR, ChangeList
|
from django.contrib.admin.views.main import ALL_VAR, SEARCH_VAR
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.template import Context, Template
|
from django.template import Context, Template
|
||||||
|
@ -30,22 +30,15 @@ from .models import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_changelist_args(modeladmin, **kwargs):
|
def build_tbody_html(pk, href, extra_fields):
|
||||||
m = modeladmin
|
return (
|
||||||
args = (
|
'<tbody><tr class="row1">'
|
||||||
kwargs.pop('list_display', m.list_display),
|
'<td class="action-checkbox">'
|
||||||
kwargs.pop('list_display_links', m.list_display_links),
|
'<input type="checkbox" name="_selected_action" value="{}" '
|
||||||
kwargs.pop('list_filter', m.list_filter),
|
'class="action-select" /></td>'
|
||||||
kwargs.pop('date_hierarchy', m.date_hierarchy),
|
'<th class="field-name"><a href="{}">name</a></th>'
|
||||||
kwargs.pop('search_fields', m.search_fields),
|
'{}</tr></tbody>'
|
||||||
kwargs.pop('list_select_related', m.list_select_related),
|
).format(pk, href, extra_fields)
|
||||||
kwargs.pop('list_per_page', m.list_per_page),
|
|
||||||
kwargs.pop('list_max_show_all', m.list_max_show_all),
|
|
||||||
kwargs.pop('list_editable', m.list_editable),
|
|
||||||
m,
|
|
||||||
)
|
|
||||||
assert not kwargs, "Unexpected kwarg %s" % kwargs
|
|
||||||
return args
|
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF="admin_changelist.urls")
|
@override_settings(ROOT_URLCONF="admin_changelist.urls")
|
||||||
|
@ -69,29 +62,20 @@ class ChangeListTests(TestCase):
|
||||||
"""
|
"""
|
||||||
m = ChildAdmin(Child, custom_site)
|
m = ChildAdmin(Child, custom_site)
|
||||||
request = self.factory.get('/child/')
|
request = self.factory.get('/child/')
|
||||||
cl = ChangeList(
|
cl = m.get_changelist_instance(request)
|
||||||
request, Child,
|
|
||||||
*get_changelist_args(m, list_select_related=m.get_list_select_related(request))
|
|
||||||
)
|
|
||||||
self.assertEqual(cl.queryset.query.select_related, {'parent': {}})
|
self.assertEqual(cl.queryset.query.select_related, {'parent': {}})
|
||||||
|
|
||||||
def test_select_related_as_tuple(self):
|
def test_select_related_as_tuple(self):
|
||||||
ia = InvitationAdmin(Invitation, custom_site)
|
ia = InvitationAdmin(Invitation, custom_site)
|
||||||
request = self.factory.get('/invitation/')
|
request = self.factory.get('/invitation/')
|
||||||
cl = ChangeList(
|
cl = ia.get_changelist_instance(request)
|
||||||
request, Child,
|
|
||||||
*get_changelist_args(ia, list_select_related=ia.get_list_select_related(request))
|
|
||||||
)
|
|
||||||
self.assertEqual(cl.queryset.query.select_related, {'player': {}})
|
self.assertEqual(cl.queryset.query.select_related, {'player': {}})
|
||||||
|
|
||||||
def test_select_related_as_empty_tuple(self):
|
def test_select_related_as_empty_tuple(self):
|
||||||
ia = InvitationAdmin(Invitation, custom_site)
|
ia = InvitationAdmin(Invitation, custom_site)
|
||||||
ia.list_select_related = ()
|
ia.list_select_related = ()
|
||||||
request = self.factory.get('/invitation/')
|
request = self.factory.get('/invitation/')
|
||||||
cl = ChangeList(
|
cl = ia.get_changelist_instance(request)
|
||||||
request, Child,
|
|
||||||
*get_changelist_args(ia, list_select_related=ia.get_list_select_related(request))
|
|
||||||
)
|
|
||||||
self.assertIs(cl.queryset.query.select_related, False)
|
self.assertIs(cl.queryset.query.select_related, False)
|
||||||
|
|
||||||
def test_get_select_related_custom_method(self):
|
def test_get_select_related_custom_method(self):
|
||||||
|
@ -103,10 +87,7 @@ class ChangeListTests(TestCase):
|
||||||
|
|
||||||
ia = GetListSelectRelatedAdmin(Invitation, custom_site)
|
ia = GetListSelectRelatedAdmin(Invitation, custom_site)
|
||||||
request = self.factory.get('/invitation/')
|
request = self.factory.get('/invitation/')
|
||||||
cl = ChangeList(
|
cl = ia.get_changelist_instance(request)
|
||||||
request, Child,
|
|
||||||
*get_changelist_args(ia, list_select_related=ia.get_list_select_related(request))
|
|
||||||
)
|
|
||||||
self.assertEqual(cl.queryset.query.select_related, {'player': {}, 'band': {}})
|
self.assertEqual(cl.queryset.query.select_related, {'player': {}, 'band': {}})
|
||||||
|
|
||||||
def test_result_list_empty_changelist_value(self):
|
def test_result_list_empty_changelist_value(self):
|
||||||
|
@ -117,16 +98,13 @@ class ChangeListTests(TestCase):
|
||||||
new_child = Child.objects.create(name='name', parent=None)
|
new_child = Child.objects.create(name='name', parent=None)
|
||||||
request = self.factory.get('/child/')
|
request = self.factory.get('/child/')
|
||||||
m = ChildAdmin(Child, custom_site)
|
m = ChildAdmin(Child, custom_site)
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.formset = None
|
cl.formset = None
|
||||||
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
||||||
context = Context({'cl': cl})
|
context = Context({'cl': cl})
|
||||||
table_output = template.render(context)
|
table_output = template.render(context)
|
||||||
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
||||||
row_html = (
|
row_html = build_tbody_html(new_child.id, link, '<td class="field-parent nowrap">-</td>')
|
||||||
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
||||||
'<td class="field-parent nowrap">-</td></tr></tbody>' % link
|
|
||||||
)
|
|
||||||
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
||||||
|
|
||||||
def test_result_list_set_empty_value_display_on_admin_site(self):
|
def test_result_list_set_empty_value_display_on_admin_site(self):
|
||||||
|
@ -138,16 +116,13 @@ class ChangeListTests(TestCase):
|
||||||
# Set a new empty display value on AdminSite.
|
# Set a new empty display value on AdminSite.
|
||||||
admin.site.empty_value_display = '???'
|
admin.site.empty_value_display = '???'
|
||||||
m = ChildAdmin(Child, admin.site)
|
m = ChildAdmin(Child, admin.site)
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.formset = None
|
cl.formset = None
|
||||||
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
||||||
context = Context({'cl': cl})
|
context = Context({'cl': cl})
|
||||||
table_output = template.render(context)
|
table_output = template.render(context)
|
||||||
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
||||||
row_html = (
|
row_html = build_tbody_html(new_child.id, link, '<td class="field-parent nowrap">???</td>')
|
||||||
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
||||||
'<td class="field-parent nowrap">???</td></tr></tbody>' % link
|
|
||||||
)
|
|
||||||
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
||||||
|
|
||||||
def test_result_list_set_empty_value_display_in_model_admin(self):
|
def test_result_list_set_empty_value_display_in_model_admin(self):
|
||||||
|
@ -157,15 +132,17 @@ class ChangeListTests(TestCase):
|
||||||
new_child = Child.objects.create(name='name', parent=None)
|
new_child = Child.objects.create(name='name', parent=None)
|
||||||
request = self.factory.get('/child/')
|
request = self.factory.get('/child/')
|
||||||
m = EmptyValueChildAdmin(Child, admin.site)
|
m = EmptyValueChildAdmin(Child, admin.site)
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.formset = None
|
cl.formset = None
|
||||||
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
||||||
context = Context({'cl': cl})
|
context = Context({'cl': cl})
|
||||||
table_output = template.render(context)
|
table_output = template.render(context)
|
||||||
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
||||||
row_html = (
|
row_html = build_tbody_html(
|
||||||
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
new_child.id,
|
||||||
'<td class="field-age_display">&dagger;</td><td class="field-age">-empty-</td></tr></tbody>' % link
|
link,
|
||||||
|
'<td class="field-age_display">&dagger;</td>'
|
||||||
|
'<td class="field-age">-empty-</td>'
|
||||||
)
|
)
|
||||||
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
||||||
|
|
||||||
|
@ -178,16 +155,13 @@ class ChangeListTests(TestCase):
|
||||||
new_child = Child.objects.create(name='name', parent=new_parent)
|
new_child = Child.objects.create(name='name', parent=new_parent)
|
||||||
request = self.factory.get('/child/')
|
request = self.factory.get('/child/')
|
||||||
m = ChildAdmin(Child, custom_site)
|
m = ChildAdmin(Child, custom_site)
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.formset = None
|
cl.formset = None
|
||||||
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
||||||
context = Context({'cl': cl})
|
context = Context({'cl': cl})
|
||||||
table_output = template.render(context)
|
table_output = template.render(context)
|
||||||
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
||||||
row_html = (
|
row_html = build_tbody_html(new_child.id, link, '<td class="field-parent nowrap">%s</td>' % new_parent)
|
||||||
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
||||||
'<td class="field-parent nowrap">%s</td></tr></tbody>' % (link, new_parent)
|
|
||||||
)
|
|
||||||
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)
|
||||||
|
|
||||||
def test_result_list_editable_html(self):
|
def test_result_list_editable_html(self):
|
||||||
|
@ -208,7 +182,7 @@ class ChangeListTests(TestCase):
|
||||||
m.list_display = ['id', 'name', 'parent']
|
m.list_display = ['id', 'name', 'parent']
|
||||||
m.list_display_links = ['id']
|
m.list_display_links = ['id']
|
||||||
m.list_editable = ['name']
|
m.list_editable = ['name']
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
FormSet = m.get_changelist_formset(request)
|
FormSet = m.get_changelist_formset(request)
|
||||||
cl.formset = FormSet(queryset=cl.result_list)
|
cl.formset = FormSet(queryset=cl.result_list)
|
||||||
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
||||||
|
@ -248,7 +222,7 @@ class ChangeListTests(TestCase):
|
||||||
m.list_display_links = ['id']
|
m.list_display_links = ['id']
|
||||||
m.list_editable = ['name']
|
m.list_editable = ['name']
|
||||||
with self.assertRaises(IncorrectLookupParameters):
|
with self.assertRaises(IncorrectLookupParameters):
|
||||||
ChangeList(request, Child, *get_changelist_args(m))
|
m.get_changelist_instance(request)
|
||||||
|
|
||||||
def test_custom_paginator(self):
|
def test_custom_paginator(self):
|
||||||
new_parent = Parent.objects.create(name='parent')
|
new_parent = Parent.objects.create(name='parent')
|
||||||
|
@ -258,7 +232,7 @@ class ChangeListTests(TestCase):
|
||||||
request = self.factory.get('/child/')
|
request = self.factory.get('/child/')
|
||||||
m = CustomPaginationAdmin(Child, custom_site)
|
m = CustomPaginationAdmin(Child, custom_site)
|
||||||
|
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
self.assertIsInstance(cl.paginator, CustomPaginator)
|
self.assertIsInstance(cl.paginator, CustomPaginator)
|
||||||
|
|
||||||
|
@ -276,7 +250,7 @@ class ChangeListTests(TestCase):
|
||||||
m = BandAdmin(Band, custom_site)
|
m = BandAdmin(Band, custom_site)
|
||||||
request = self.factory.get('/band/', data={'genres': blues.pk})
|
request = self.factory.get('/band/', data={'genres': blues.pk})
|
||||||
|
|
||||||
cl = ChangeList(request, Band, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
|
|
||||||
# There's only one Group instance
|
# There's only one Group instance
|
||||||
|
@ -295,7 +269,7 @@ class ChangeListTests(TestCase):
|
||||||
m = GroupAdmin(Group, custom_site)
|
m = GroupAdmin(Group, custom_site)
|
||||||
request = self.factory.get('/group/', data={'members': lead.pk})
|
request = self.factory.get('/group/', data={'members': lead.pk})
|
||||||
|
|
||||||
cl = ChangeList(request, Group, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
|
|
||||||
# There's only one Group instance
|
# There's only one Group instance
|
||||||
|
@ -316,7 +290,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ConcertAdmin(Concert, custom_site)
|
m = ConcertAdmin(Concert, custom_site)
|
||||||
request = self.factory.get('/concert/', data={'group__members': lead.pk})
|
request = self.factory.get('/concert/', data={'group__members': lead.pk})
|
||||||
|
|
||||||
cl = ChangeList(request, Concert, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
|
|
||||||
# There's only one Concert instance
|
# There's only one Concert instance
|
||||||
|
@ -336,7 +310,7 @@ class ChangeListTests(TestCase):
|
||||||
m = QuartetAdmin(Quartet, custom_site)
|
m = QuartetAdmin(Quartet, custom_site)
|
||||||
request = self.factory.get('/quartet/', data={'members': lead.pk})
|
request = self.factory.get('/quartet/', data={'members': lead.pk})
|
||||||
|
|
||||||
cl = ChangeList(request, Quartet, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
|
|
||||||
# There's only one Quartet instance
|
# There's only one Quartet instance
|
||||||
|
@ -356,7 +330,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ChordsBandAdmin(ChordsBand, custom_site)
|
m = ChordsBandAdmin(ChordsBand, custom_site)
|
||||||
request = self.factory.get('/chordsband/', data={'members': lead.pk})
|
request = self.factory.get('/chordsband/', data={'members': lead.pk})
|
||||||
|
|
||||||
cl = ChangeList(request, ChordsBand, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
|
|
||||||
# There's only one ChordsBand instance
|
# There's only one ChordsBand instance
|
||||||
|
@ -375,7 +349,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ParentAdmin(Parent, custom_site)
|
m = ParentAdmin(Parent, custom_site)
|
||||||
request = self.factory.get('/parent/', data={'child__name': 'Daniel'})
|
request = self.factory.get('/parent/', data={'child__name': 'Daniel'})
|
||||||
|
|
||||||
cl = ChangeList(request, Parent, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
# Make sure distinct() was called
|
# Make sure distinct() was called
|
||||||
self.assertEqual(cl.queryset.count(), 1)
|
self.assertEqual(cl.queryset.count(), 1)
|
||||||
|
|
||||||
|
@ -391,7 +365,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ParentAdmin(Parent, custom_site)
|
m = ParentAdmin(Parent, custom_site)
|
||||||
request = self.factory.get('/parent/', data={SEARCH_VAR: 'daniel'})
|
request = self.factory.get('/parent/', data={SEARCH_VAR: 'daniel'})
|
||||||
|
|
||||||
cl = ChangeList(request, Parent, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
# Make sure distinct() was called
|
# Make sure distinct() was called
|
||||||
self.assertEqual(cl.queryset.count(), 1)
|
self.assertEqual(cl.queryset.count(), 1)
|
||||||
|
|
||||||
|
@ -410,7 +384,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ConcertAdmin(Concert, custom_site)
|
m = ConcertAdmin(Concert, custom_site)
|
||||||
request = self.factory.get('/concert/', data={SEARCH_VAR: 'vox'})
|
request = self.factory.get('/concert/', data={SEARCH_VAR: 'vox'})
|
||||||
|
|
||||||
cl = ChangeList(request, Concert, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
# There's only one Concert instance
|
# There's only one Concert instance
|
||||||
self.assertEqual(cl.queryset.count(), 1)
|
self.assertEqual(cl.queryset.count(), 1)
|
||||||
|
|
||||||
|
@ -422,11 +396,11 @@ class ChangeListTests(TestCase):
|
||||||
m.search_fields = ['group__pk']
|
m.search_fields = ['group__pk']
|
||||||
|
|
||||||
request = self.factory.get('/concert/', data={SEARCH_VAR: band.pk})
|
request = self.factory.get('/concert/', data={SEARCH_VAR: band.pk})
|
||||||
cl = ChangeList(request, Concert, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
self.assertEqual(cl.queryset.count(), 1)
|
self.assertEqual(cl.queryset.count(), 1)
|
||||||
|
|
||||||
request = self.factory.get('/concert/', data={SEARCH_VAR: band.pk + 5})
|
request = self.factory.get('/concert/', data={SEARCH_VAR: band.pk + 5})
|
||||||
cl = ChangeList(request, Concert, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
self.assertEqual(cl.queryset.count(), 0)
|
self.assertEqual(cl.queryset.count(), 0)
|
||||||
|
|
||||||
def test_no_distinct_for_m2m_in_list_filter_without_params(self):
|
def test_no_distinct_for_m2m_in_list_filter_without_params(self):
|
||||||
|
@ -437,12 +411,12 @@ class ChangeListTests(TestCase):
|
||||||
m = BandAdmin(Band, custom_site)
|
m = BandAdmin(Band, custom_site)
|
||||||
for lookup_params in ({}, {'name': 'test'}):
|
for lookup_params in ({}, {'name': 'test'}):
|
||||||
request = self.factory.get('/band/', lookup_params)
|
request = self.factory.get('/band/', lookup_params)
|
||||||
cl = ChangeList(request, Band, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
self.assertFalse(cl.queryset.query.distinct)
|
self.assertFalse(cl.queryset.query.distinct)
|
||||||
|
|
||||||
# A ManyToManyField in params does have distinct applied.
|
# A ManyToManyField in params does have distinct applied.
|
||||||
request = self.factory.get('/band/', {'genres': '0'})
|
request = self.factory.get('/band/', {'genres': '0'})
|
||||||
cl = ChangeList(request, Band, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
self.assertTrue(cl.queryset.query.distinct)
|
self.assertTrue(cl.queryset.query.distinct)
|
||||||
|
|
||||||
def test_pagination(self):
|
def test_pagination(self):
|
||||||
|
@ -459,14 +433,14 @@ class ChangeListTests(TestCase):
|
||||||
|
|
||||||
# Test default queryset
|
# Test default queryset
|
||||||
m = ChildAdmin(Child, custom_site)
|
m = ChildAdmin(Child, custom_site)
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
self.assertEqual(cl.queryset.count(), 60)
|
self.assertEqual(cl.queryset.count(), 60)
|
||||||
self.assertEqual(cl.paginator.count, 60)
|
self.assertEqual(cl.paginator.count, 60)
|
||||||
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3, 4, 5, 6])
|
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3, 4, 5, 6])
|
||||||
|
|
||||||
# Test custom queryset
|
# Test custom queryset
|
||||||
m = FilteredChildAdmin(Child, custom_site)
|
m = FilteredChildAdmin(Child, custom_site)
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
self.assertEqual(cl.queryset.count(), 30)
|
self.assertEqual(cl.queryset.count(), 30)
|
||||||
self.assertEqual(cl.paginator.count, 30)
|
self.assertEqual(cl.paginator.count, 30)
|
||||||
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3])
|
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3])
|
||||||
|
@ -538,7 +512,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ChildAdmin(Child, custom_site)
|
m = ChildAdmin(Child, custom_site)
|
||||||
m.list_max_show_all = 200
|
m.list_max_show_all = 200
|
||||||
# 200 is the max we'll pass to ChangeList
|
# 200 is the max we'll pass to ChangeList
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
self.assertEqual(len(cl.result_list), 60)
|
self.assertEqual(len(cl.result_list), 60)
|
||||||
|
|
||||||
|
@ -547,7 +521,7 @@ class ChangeListTests(TestCase):
|
||||||
m = ChildAdmin(Child, custom_site)
|
m = ChildAdmin(Child, custom_site)
|
||||||
m.list_max_show_all = 30
|
m.list_max_show_all = 30
|
||||||
# 30 is the max we'll pass to ChangeList for this test
|
# 30 is the max we'll pass to ChangeList for this test
|
||||||
cl = ChangeList(request, Child, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
cl.get_results(request)
|
cl.get_results(request)
|
||||||
self.assertEqual(len(cl.result_list), 10)
|
self.assertEqual(len(cl.result_list), 10)
|
||||||
|
|
||||||
|
@ -792,7 +766,7 @@ class ChangeListTests(TestCase):
|
||||||
# instantiating and setting up ChangeList object
|
# instantiating and setting up ChangeList object
|
||||||
m = GroupAdmin(Group, custom_site)
|
m = GroupAdmin(Group, custom_site)
|
||||||
request = self.factory.get('/group/')
|
request = self.factory.get('/group/')
|
||||||
cl = ChangeList(request, Group, *get_changelist_args(m))
|
cl = m.get_changelist_instance(request)
|
||||||
per_page = cl.list_per_page = 10
|
per_page = cl.list_per_page = 10
|
||||||
|
|
||||||
for page_num, objects_count, expected_page_range in [
|
for page_num, objects_count, expected_page_range in [
|
||||||
|
|
|
@ -7,7 +7,6 @@ from django.contrib.admin import (
|
||||||
RelatedOnlyFieldListFilter, SimpleListFilter, site,
|
RelatedOnlyFieldListFilter, SimpleListFilter, site,
|
||||||
)
|
)
|
||||||
from django.contrib.admin.options import IncorrectLookupParameters
|
from django.contrib.admin.options import IncorrectLookupParameters
|
||||||
from django.contrib.admin.views.main import ChangeList
|
|
||||||
from django.contrib.auth.admin import UserAdmin
|
from django.contrib.auth.admin import UserAdmin
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
@ -291,15 +290,6 @@ class ListFiltersTests(TestCase):
|
||||||
self.john = Employee.objects.create(name='John Blue', department=self.dev)
|
self.john = Employee.objects.create(name='John Blue', department=self.dev)
|
||||||
self.jack = Employee.objects.create(name='Jack Red', department=self.design)
|
self.jack = Employee.objects.create(name='Jack Red', department=self.design)
|
||||||
|
|
||||||
def get_changelist(self, request, model, modeladmin):
|
|
||||||
return ChangeList(
|
|
||||||
request, model, modeladmin.list_display,
|
|
||||||
modeladmin.list_display_links, modeladmin.list_filter,
|
|
||||||
modeladmin.date_hierarchy, modeladmin.search_fields,
|
|
||||||
modeladmin.list_select_related, modeladmin.list_per_page,
|
|
||||||
modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin,
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_choicesfieldlistfilter_has_none_choice(self):
|
def test_choicesfieldlistfilter_has_none_choice(self):
|
||||||
"""
|
"""
|
||||||
The last choice is for the None value.
|
The last choice is for the None value.
|
||||||
|
@ -310,7 +300,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
modeladmin = BookmarkChoicesAdmin(Bookmark, site)
|
modeladmin = BookmarkChoicesAdmin(Bookmark, site)
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
changelist = self.get_changelist(request, Bookmark, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
filterspec = changelist.get_filters(request)[0][0]
|
filterspec = changelist.get_filters(request)[0][0]
|
||||||
choices = list(filterspec.choices(changelist))
|
choices = list(filterspec.choices(changelist))
|
||||||
self.assertEqual(choices[-1]['display'], 'None')
|
self.assertEqual(choices[-1]['display'], 'None')
|
||||||
|
@ -320,11 +310,11 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdmin(Book, site)
|
modeladmin = BookAdmin(Book, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist(request)
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'date_registered__gte': self.today,
|
request = self.request_factory.get('/', {'date_registered__gte': self.today,
|
||||||
'date_registered__lt': self.tomorrow})
|
'date_registered__lt': self.tomorrow})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -345,7 +335,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(day=1),
|
request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(day=1),
|
||||||
'date_registered__lt': self.next_month})
|
'date_registered__lt': self.next_month})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -370,7 +360,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(month=1, day=1),
|
request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(month=1, day=1),
|
||||||
'date_registered__lt': self.next_year})
|
'date_registered__lt': self.next_year})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -397,7 +387,7 @@ class ListFiltersTests(TestCase):
|
||||||
'date_registered__gte': str(self.one_week_ago),
|
'date_registered__gte': str(self.one_week_ago),
|
||||||
'date_registered__lt': str(self.tomorrow),
|
'date_registered__lt': str(self.tomorrow),
|
||||||
})
|
})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -418,7 +408,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# Null/not null queries
|
# Null/not null queries
|
||||||
request = self.request_factory.get('/', {'date_registered__isnull': 'True'})
|
request = self.request_factory.get('/', {'date_registered__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -433,7 +423,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choice['query_string'], '?date_registered__isnull=True')
|
self.assertEqual(choice['query_string'], '?date_registered__isnull=True')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'date_registered__isnull': 'False'})
|
request = self.request_factory.get('/', {'date_registered__isnull': 'False'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -461,7 +451,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdmin(Book, site)
|
modeladmin = BookAdmin(Book, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'year__isnull': 'True'})
|
request = self.request_factory.get('/', {'year__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -475,7 +465,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choices[-1]['query_string'], '?year__isnull=True')
|
self.assertEqual(choices[-1]['query_string'], '?year__isnull=True')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'year': '2002'})
|
request = self.request_factory.get('/', {'year': '2002'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct choice is selected
|
# Make sure the correct choice is selected
|
||||||
filterspec = changelist.get_filters(request)[0][0]
|
filterspec = changelist.get_filters(request)[0][0]
|
||||||
|
@ -488,7 +478,7 @@ class ListFiltersTests(TestCase):
|
||||||
# Make sure that correct filters are returned with custom querysets
|
# Make sure that correct filters are returned with custom querysets
|
||||||
modeladmin = BookAdminWithCustomQueryset(self.alfred, Book, site)
|
modeladmin = BookAdminWithCustomQueryset(self.alfred, Book, site)
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
filterspec = changelist.get_filters(request)[0][0]
|
filterspec = changelist.get_filters(request)[0][0]
|
||||||
choices = list(filterspec.choices(changelist))
|
choices = list(filterspec.choices(changelist))
|
||||||
|
@ -504,7 +494,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdmin(Book, site)
|
modeladmin = BookAdmin(Book, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure that all users are present in the author's list filter
|
# Make sure that all users are present in the author's list filter
|
||||||
filterspec = changelist.get_filters(request)[0][1]
|
filterspec = changelist.get_filters(request)[0][1]
|
||||||
|
@ -512,7 +502,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected))
|
self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected))
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'author__isnull': 'True'})
|
request = self.request_factory.get('/', {'author__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -526,7 +516,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')
|
self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk})
|
request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct choice is selected
|
# Make sure the correct choice is selected
|
||||||
filterspec = changelist.get_filters(request)[0][1]
|
filterspec = changelist.get_filters(request)[0][1]
|
||||||
|
@ -540,7 +530,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdmin(Book, site)
|
modeladmin = BookAdmin(Book, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure that all users are present in the contrib's list filter
|
# Make sure that all users are present in the contrib's list filter
|
||||||
filterspec = changelist.get_filters(request)[0][2]
|
filterspec = changelist.get_filters(request)[0][2]
|
||||||
|
@ -548,7 +538,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected))
|
self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected))
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'contributors__isnull': 'True'})
|
request = self.request_factory.get('/', {'contributors__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -562,7 +552,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')
|
self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk})
|
request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct choice is selected
|
# Make sure the correct choice is selected
|
||||||
filterspec = changelist.get_filters(request)[0][2]
|
filterspec = changelist.get_filters(request)[0][2]
|
||||||
|
@ -576,7 +566,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# FK relationship -----
|
# FK relationship -----
|
||||||
request = self.request_factory.get('/', {'books_authored__isnull': 'True'})
|
request = self.request_factory.get('/', {'books_authored__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, User, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -590,7 +580,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True')
|
self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'books_authored__id__exact': self.bio_book.pk})
|
request = self.request_factory.get('/', {'books_authored__id__exact': self.bio_book.pk})
|
||||||
changelist = self.get_changelist(request, User, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct choice is selected
|
# Make sure the correct choice is selected
|
||||||
filterspec = changelist.get_filters(request)[0][0]
|
filterspec = changelist.get_filters(request)[0][0]
|
||||||
|
@ -601,7 +591,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# M2M relationship -----
|
# M2M relationship -----
|
||||||
request = self.request_factory.get('/', {'books_contributed__isnull': 'True'})
|
request = self.request_factory.get('/', {'books_contributed__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, User, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -615,7 +605,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True')
|
self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'books_contributed__id__exact': self.django_book.pk})
|
request = self.request_factory.get('/', {'books_contributed__id__exact': self.django_book.pk})
|
||||||
changelist = self.get_changelist(request, User, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct choice is selected
|
# Make sure the correct choice is selected
|
||||||
filterspec = changelist.get_filters(request)[0][1]
|
filterspec = changelist.get_filters(request)[0][1]
|
||||||
|
@ -638,7 +628,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdminRelatedOnlyFilter(Book, site)
|
modeladmin = BookAdminRelatedOnlyFilter(Book, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure that only actual authors are present in author's list filter
|
# Make sure that only actual authors are present in author's list filter
|
||||||
filterspec = changelist.get_filters(request)[0][4]
|
filterspec = changelist.get_filters(request)[0][4]
|
||||||
|
@ -654,7 +644,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
modeladmin = BookAdminRelatedOnlyFilter(Book, site)
|
modeladmin = BookAdminRelatedOnlyFilter(Book, site)
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Only actual departments should be present in employee__department's
|
# Only actual departments should be present in employee__department's
|
||||||
# list filter.
|
# list filter.
|
||||||
|
@ -669,7 +659,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdminRelatedOnlyFilter(Book, site)
|
modeladmin = BookAdminRelatedOnlyFilter(Book, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure that only actual contributors are present in contrib's list filter
|
# Make sure that only actual contributors are present in contrib's list filter
|
||||||
filterspec = changelist.get_filters(request)[0][5]
|
filterspec = changelist.get_filters(request)[0][5]
|
||||||
|
@ -688,7 +678,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookmarkAdminGenericRelation(Bookmark, site)
|
modeladmin = BookmarkAdminGenericRelation(Bookmark, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'tags__tag': 'python'})
|
request = self.request_factory.get('/', {'tags__tag': 'python'})
|
||||||
changelist = self.get_changelist(request, Bookmark, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
|
||||||
expected = [python_bookmark, django_bookmark]
|
expected = [python_bookmark, django_bookmark]
|
||||||
|
@ -704,10 +694,10 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
def verify_booleanfieldlistfilter(self, modeladmin):
|
def verify_booleanfieldlistfilter(self, modeladmin):
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'is_best_seller__exact': 0})
|
request = self.request_factory.get('/', {'is_best_seller__exact': 0})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -721,7 +711,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choice['query_string'], '?is_best_seller__exact=0')
|
self.assertEqual(choice['query_string'], '?is_best_seller__exact=0')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'is_best_seller__exact': 1})
|
request = self.request_factory.get('/', {'is_best_seller__exact': 1})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -735,7 +725,7 @@ class ListFiltersTests(TestCase):
|
||||||
self.assertEqual(choice['query_string'], '?is_best_seller__exact=1')
|
self.assertEqual(choice['query_string'], '?is_best_seller__exact=1')
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'is_best_seller__isnull': 'True'})
|
request = self.request_factory.get('/', {'is_best_seller__isnull': 'True'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -755,10 +745,10 @@ class ListFiltersTests(TestCase):
|
||||||
"""
|
"""
|
||||||
modeladmin = BookAdminWithUnderscoreLookupAndTuple(Book, site)
|
modeladmin = BookAdminWithUnderscoreLookupAndTuple(Book, site)
|
||||||
request = self.request_factory.get('/')
|
request = self.request_factory.get('/')
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'author__email': 'alfred@example.com'})
|
request = self.request_factory.get('/', {'author__email': 'alfred@example.com'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -769,14 +759,14 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = BookAdmin(Book, site)
|
modeladmin = BookAdmin(Book, site)
|
||||||
request = self.request_factory.get('/', {'author__id__exact': 'StringNotInteger!'})
|
request = self.request_factory.get('/', {'author__id__exact': 'StringNotInteger!'})
|
||||||
with self.assertRaises(IncorrectLookupParameters):
|
with self.assertRaises(IncorrectLookupParameters):
|
||||||
self.get_changelist(request, Book, modeladmin)
|
modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
def test_simplelistfilter(self):
|
def test_simplelistfilter(self):
|
||||||
modeladmin = DecadeFilterBookAdmin(Book, site)
|
modeladmin = DecadeFilterBookAdmin(Book, site)
|
||||||
|
|
||||||
# Make sure that the first option is 'All' ---------------------------
|
# Make sure that the first option is 'All' ---------------------------
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -792,7 +782,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# Look for books in the 1980s ----------------------------------------
|
# Look for books in the 1980s ----------------------------------------
|
||||||
request = self.request_factory.get('/', {'publication-decade': 'the 80s'})
|
request = self.request_factory.get('/', {'publication-decade': 'the 80s'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -808,7 +798,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# Look for books in the 1990s ----------------------------------------
|
# Look for books in the 1990s ----------------------------------------
|
||||||
request = self.request_factory.get('/', {'publication-decade': 'the 90s'})
|
request = self.request_factory.get('/', {'publication-decade': 'the 90s'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -824,7 +814,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# Look for books in the 2000s ----------------------------------------
|
# Look for books in the 2000s ----------------------------------------
|
||||||
request = self.request_factory.get('/', {'publication-decade': 'the 00s'})
|
request = self.request_factory.get('/', {'publication-decade': 'the 00s'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -840,7 +830,7 @@ class ListFiltersTests(TestCase):
|
||||||
|
|
||||||
# Combine multiple filters -------------------------------------------
|
# Combine multiple filters -------------------------------------------
|
||||||
request = self.request_factory.get('/', {'publication-decade': 'the 00s', 'author__id__exact': self.alfred.pk})
|
request = self.request_factory.get('/', {'publication-decade': 'the 00s', 'author__id__exact': self.alfred.pk})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -871,7 +861,7 @@ class ListFiltersTests(TestCase):
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
msg = "The list filter 'DecadeListFilterWithoutTitle' does not specify a 'title'."
|
msg = "The list filter 'DecadeListFilterWithoutTitle' does not specify a 'title'."
|
||||||
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
||||||
self.get_changelist(request, Book, modeladmin)
|
modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
def test_simplelistfilter_without_parameter(self):
|
def test_simplelistfilter_without_parameter(self):
|
||||||
"""
|
"""
|
||||||
|
@ -881,7 +871,7 @@ class ListFiltersTests(TestCase):
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
msg = "The list filter 'DecadeListFilterWithoutParameter' does not specify a 'parameter_name'."
|
msg = "The list filter 'DecadeListFilterWithoutParameter' does not specify a 'parameter_name'."
|
||||||
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
||||||
self.get_changelist(request, Book, modeladmin)
|
modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
def test_simplelistfilter_with_none_returning_lookups(self):
|
def test_simplelistfilter_with_none_returning_lookups(self):
|
||||||
"""
|
"""
|
||||||
|
@ -890,7 +880,7 @@ class ListFiltersTests(TestCase):
|
||||||
"""
|
"""
|
||||||
modeladmin = DecadeFilterBookAdminWithNoneReturningLookups(Book, site)
|
modeladmin = DecadeFilterBookAdminWithNoneReturningLookups(Book, site)
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
filterspec = changelist.get_filters(request)[0]
|
filterspec = changelist.get_filters(request)[0]
|
||||||
self.assertEqual(len(filterspec), 0)
|
self.assertEqual(len(filterspec), 0)
|
||||||
|
|
||||||
|
@ -902,12 +892,12 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = DecadeFilterBookAdminWithFailingQueryset(Book, site)
|
modeladmin = DecadeFilterBookAdminWithFailingQueryset(Book, site)
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
with self.assertRaises(ZeroDivisionError):
|
with self.assertRaises(ZeroDivisionError):
|
||||||
self.get_changelist(request, Book, modeladmin)
|
modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
def test_simplelistfilter_with_queryset_based_lookups(self):
|
def test_simplelistfilter_with_queryset_based_lookups(self):
|
||||||
modeladmin = DecadeFilterBookAdminWithQuerysetBasedLookups(Book, site)
|
modeladmin = DecadeFilterBookAdminWithQuerysetBasedLookups(Book, site)
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
filterspec = changelist.get_filters(request)[0][0]
|
filterspec = changelist.get_filters(request)[0][0]
|
||||||
self.assertEqual(filterspec.title, 'publication decade')
|
self.assertEqual(filterspec.title, 'publication decade')
|
||||||
|
@ -932,7 +922,7 @@ class ListFiltersTests(TestCase):
|
||||||
"""
|
"""
|
||||||
modeladmin = BookAdmin(Book, site)
|
modeladmin = BookAdmin(Book, site)
|
||||||
request = self.request_factory.get('/', {'no': '207'})
|
request = self.request_factory.get('/', {'no': '207'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -952,7 +942,7 @@ class ListFiltersTests(TestCase):
|
||||||
# When it ends with '__in' -----------------------------------------
|
# When it ends with '__in' -----------------------------------------
|
||||||
modeladmin = DecadeFilterBookAdminParameterEndsWith__In(Book, site)
|
modeladmin = DecadeFilterBookAdminParameterEndsWith__In(Book, site)
|
||||||
request = self.request_factory.get('/', {'decade__in': 'the 90s'})
|
request = self.request_factory.get('/', {'decade__in': 'the 90s'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -969,7 +959,7 @@ class ListFiltersTests(TestCase):
|
||||||
# When it ends with '__isnull' ---------------------------------------
|
# When it ends with '__isnull' ---------------------------------------
|
||||||
modeladmin = DecadeFilterBookAdminParameterEndsWith__Isnull(Book, site)
|
modeladmin = DecadeFilterBookAdminParameterEndsWith__Isnull(Book, site)
|
||||||
request = self.request_factory.get('/', {'decade__isnull': 'the 90s'})
|
request = self.request_factory.get('/', {'decade__isnull': 'the 90s'})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -990,7 +980,7 @@ class ListFiltersTests(TestCase):
|
||||||
"""
|
"""
|
||||||
modeladmin = DepartmentFilterEmployeeAdmin(Employee, site)
|
modeladmin = DepartmentFilterEmployeeAdmin(Employee, site)
|
||||||
request = self.request_factory.get('/', {'department': self.john.department.pk})
|
request = self.request_factory.get('/', {'department': self.john.department.pk})
|
||||||
changelist = self.get_changelist(request, Employee, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
|
||||||
|
@ -1010,7 +1000,7 @@ class ListFiltersTests(TestCase):
|
||||||
"""
|
"""
|
||||||
modeladmin = DepartmentFilterUnderscoredEmployeeAdmin(Employee, site)
|
modeladmin = DepartmentFilterUnderscoredEmployeeAdmin(Employee, site)
|
||||||
request = self.request_factory.get('/', {'department__whatever': self.john.department.pk})
|
request = self.request_factory.get('/', {'department__whatever': self.john.department.pk})
|
||||||
changelist = self.get_changelist(request, Employee, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
|
||||||
|
@ -1030,7 +1020,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = EmployeeAdmin(Employee, site)
|
modeladmin = EmployeeAdmin(Employee, site)
|
||||||
|
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
changelist = self.get_changelist(request, Employee, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -1055,7 +1045,7 @@ class ListFiltersTests(TestCase):
|
||||||
# Filter by Department=='Development' --------------------------------
|
# Filter by Department=='Development' --------------------------------
|
||||||
|
|
||||||
request = self.request_factory.get('/', {'department__code__exact': 'DEV'})
|
request = self.request_factory.get('/', {'department__code__exact': 'DEV'})
|
||||||
changelist = self.get_changelist(request, Employee, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
|
@ -1084,7 +1074,7 @@ class ListFiltersTests(TestCase):
|
||||||
modeladmin = DepartmentFilterDynamicValueBookAdmin(Book, site)
|
modeladmin = DepartmentFilterDynamicValueBookAdmin(Book, site)
|
||||||
|
|
||||||
def _test_choices(request, expected_displays):
|
def _test_choices(request, expected_displays):
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
filterspec = changelist.get_filters(request)[0][0]
|
filterspec = changelist.get_filters(request)[0][0]
|
||||||
self.assertEqual(filterspec.title, 'publication decade')
|
self.assertEqual(filterspec.title, 'publication decade')
|
||||||
choices = tuple(c['display'] for c in filterspec.choices(changelist))
|
choices = tuple(c['display'] for c in filterspec.choices(changelist))
|
||||||
|
@ -1106,6 +1096,6 @@ class ListFiltersTests(TestCase):
|
||||||
"""
|
"""
|
||||||
modeladmin = NotNinetiesListFilterAdmin(Book, site)
|
modeladmin = NotNinetiesListFilterAdmin(Book, site)
|
||||||
request = self.request_factory.get('/', {})
|
request = self.request_factory.get('/', {})
|
||||||
changelist = self.get_changelist(request, Book, modeladmin)
|
changelist = modeladmin.get_changelist_instance(request)
|
||||||
changelist.get_results(request)
|
changelist.get_results(request)
|
||||||
self.assertEqual(changelist.full_result_count, 4)
|
self.assertEqual(changelist.full_result_count, 4)
|
||||||
|
|
Loading…
Reference in New Issue