|
12 | 12 | from django.core.exceptions import ImproperlyConfigured |
13 | 13 | from django.test import RequestFactory, TestCase, override_settings |
14 | 14 |
|
15 | | -from .models import Book, Bookmark, Department, Employee, TaggedItem |
| 15 | +from .models import ( |
| 16 | + Book, Bookmark, Department, Employee, ImprovedBook, TaggedItem, |
| 17 | +) |
16 | 18 |
|
17 | 19 |
|
18 | 20 | def select_by(dictlist, key, value): |
@@ -252,11 +254,15 @@ class BookAdminWithEmptyFieldListFilter(ModelAdmin): |
252 | 254 | list_filter = [ |
253 | 255 | ('author', EmptyFieldListFilter), |
254 | 256 | ('title', EmptyFieldListFilter), |
| 257 | + ('improvedbook', EmptyFieldListFilter), |
255 | 258 | ] |
256 | 259 |
|
257 | 260 |
|
258 | 261 | class DepartmentAdminWithEmptyFieldListFilter(ModelAdmin): |
259 | | - list_filter = [('description', EmptyFieldListFilter)] |
| 262 | + list_filter = [ |
| 263 | + ('description', EmptyFieldListFilter), |
| 264 | + ('employee', EmptyFieldListFilter), |
| 265 | + ] |
260 | 266 |
|
261 | 267 |
|
262 | 268 | class ListFiltersTests(TestCase): |
@@ -1432,6 +1438,45 @@ def test_emptylistfieldfilter(self): |
1432 | 1438 | queryset = changelist.get_queryset(request) |
1433 | 1439 | self.assertCountEqual(queryset, expected_result) |
1434 | 1440 |
|
| 1441 | + def test_emptylistfieldfilter_reverse_relationships(self): |
| 1442 | + class UserAdminReverseRelationship(UserAdmin): |
| 1443 | + list_filter = ( |
| 1444 | + ('books_contributed', EmptyFieldListFilter), |
| 1445 | + ) |
| 1446 | + |
| 1447 | + ImprovedBook.objects.create(book=self.guitar_book) |
| 1448 | + no_employees = Department.objects.create(code='NONE', description=None) |
| 1449 | + |
| 1450 | + book_admin = BookAdminWithEmptyFieldListFilter(Book, site) |
| 1451 | + department_admin = DepartmentAdminWithEmptyFieldListFilter(Department, site) |
| 1452 | + user_admin = UserAdminReverseRelationship(User, site) |
| 1453 | + |
| 1454 | + tests = [ |
| 1455 | + # Reverse one-to-one relationship. |
| 1456 | + ( |
| 1457 | + book_admin, |
| 1458 | + {'improvedbook__isempty': '1'}, |
| 1459 | + [self.django_book, self.bio_book, self.djangonaut_book], |
| 1460 | + ), |
| 1461 | + (book_admin, {'improvedbook__isempty': '0'}, [self.guitar_book]), |
| 1462 | + # Reverse foreign key relationship. |
| 1463 | + (department_admin, {'employee__isempty': '1'}, [no_employees]), |
| 1464 | + (department_admin, {'employee__isempty': '0'}, [self.dev, self.design]), |
| 1465 | + # Reverse many-to-many relationship. |
| 1466 | + (user_admin, {'books_contributed__isempty': '1'}, [self.alfred]), |
| 1467 | + (user_admin, {'books_contributed__isempty': '0'}, [self.bob, self.lisa]), |
| 1468 | + ] |
| 1469 | + for modeladmin, query_string, expected_result in tests: |
| 1470 | + with self.subTest( |
| 1471 | + modeladmin=modeladmin.__class__.__name__, |
| 1472 | + query_string=query_string, |
| 1473 | + ): |
| 1474 | + request = self.request_factory.get('/', query_string) |
| 1475 | + request.user = self.alfred |
| 1476 | + changelist = modeladmin.get_changelist_instance(request) |
| 1477 | + queryset = changelist.get_queryset(request) |
| 1478 | + self.assertCountEqual(queryset, expected_result) |
| 1479 | + |
1435 | 1480 | def test_emptylistfieldfilter_choices(self): |
1436 | 1481 | modeladmin = BookAdminWithEmptyFieldListFilter(Book, site) |
1437 | 1482 | request = self.request_factory.get('/') |
|
0 commit comments