|
@@ -106,6 +106,17 @@ class DepartmentListFilterLookupWithUnderscoredParameter(DepartmentListFilterLoo
|
|
|
parameter_name = 'department__whatever'
|
|
|
|
|
|
|
|
|
+class DepartmentListFilterLookupWithDynamicValue(DecadeListFilterWithTitleAndParameter):
|
|
|
+
|
|
|
+ def lookups(self, request, model_admin):
|
|
|
+ if self.value() == 'the 80s':
|
|
|
+ return (('the 90s', "the 1990's"),)
|
|
|
+ elif self.value() == 'the 90s':
|
|
|
+ return (('the 80s', "the 1980's"),)
|
|
|
+ else:
|
|
|
+ return (('the 80s', "the 1980's"), ('the 90s', "the 1990's"),)
|
|
|
+
|
|
|
+
|
|
|
class CustomUserAdmin(UserAdmin):
|
|
|
list_filter = ('books_authored', 'books_contributed')
|
|
|
|
|
@@ -169,6 +180,10 @@ class DepartmentFilterUnderscoredEmployeeAdmin(EmployeeAdmin):
|
|
|
list_filter = [DepartmentListFilterLookupWithUnderscoredParameter, ]
|
|
|
|
|
|
|
|
|
+class DepartmentFilterDynamicValueBookAdmin(EmployeeAdmin):
|
|
|
+ list_filter = [DepartmentListFilterLookupWithDynamicValue, ]
|
|
|
+
|
|
|
+
|
|
|
class ListFiltersTests(TestCase):
|
|
|
|
|
|
def setUp(self):
|
|
@@ -816,3 +831,25 @@ class ListFiltersTests(TestCase):
|
|
|
self.assertEqual(choices[2]['display'], 'Design')
|
|
|
self.assertEqual(choices[2]['selected'], False)
|
|
|
self.assertEqual(choices[2]['query_string'], '?department__code__exact=DSN')
|
|
|
+
|
|
|
+ def test_lookup_with_dynamic_value(self):
|
|
|
+ """
|
|
|
+ Ensure SimpleListFilter can access self.value() inside the lookup.
|
|
|
+ """
|
|
|
+ modeladmin = DepartmentFilterDynamicValueBookAdmin(Book, site)
|
|
|
+
|
|
|
+ def _test_choices(request, expected_displays):
|
|
|
+ changelist = self.get_changelist(request, Book, modeladmin)
|
|
|
+ filterspec = changelist.get_filters(request)[0][0]
|
|
|
+ self.assertEqual(force_text(filterspec.title), 'publication decade')
|
|
|
+ choices = tuple(c['display'] for c in filterspec.choices(changelist))
|
|
|
+ self.assertEqual(choices, expected_displays)
|
|
|
+
|
|
|
+ _test_choices(self.request_factory.get('/', {}),
|
|
|
+ ("All", "the 1980's", "the 1990's"))
|
|
|
+
|
|
|
+ _test_choices(self.request_factory.get('/', {'publication-decade': 'the 80s'}),
|
|
|
+ ("All", "the 1990's"))
|
|
|
+
|
|
|
+ _test_choices(self.request_factory.get('/', {'publication-decade': 'the 90s'}),
|
|
|
+ ("All", "the 1980's"))
|