diff options
| author | Konrad Świat <konrad.swiat@gmail.com> | 2014-10-09 00:47:48 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-10-10 12:10:32 -0400 |
| commit | 06b11b617e7d3439bb36b65d1cacd4cc75f36c69 (patch) | |
| tree | 773132aab0f7eecdfb3f140775ca1f12a38dc92c /tests | |
| parent | 466b38ea3dfb49597e9668ffb14ed017a10bb09d (diff) | |
Fixed #23616 - Fixed generic relations in ModelAdmin.list_filter.
Thanks ranjur for reporting bug, timgraham for review,
and collinanderson for contributing tips.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/admin_filters/models.py | 22 | ||||
| -rw-r--r-- | tests/admin_filters/tests.py | 24 |
2 files changed, 45 insertions, 1 deletions
diff --git a/tests/admin_filters/models.py b/tests/admin_filters/models.py index 3025d1b4a4..36e098e62a 100644 --- a/tests/admin_filters/models.py +++ b/tests/admin_filters/models.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals from django.contrib.auth.models import User +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation +from django.contrib.contenttypes.models import ContentType from django.db import models from django.utils.encoding import python_2_unicode_compatible @@ -35,3 +37,23 @@ class Employee(models.Model): def __str__(self): return self.name + + +@python_2_unicode_compatible +class TaggedItem(models.Model): + tag = models.SlugField() + content_type = models.ForeignKey(ContentType, related_name='tagged_items') + object_id = models.PositiveIntegerField() + content_object = GenericForeignKey('content_type', 'object_id') + + def __str__(self): + return self.tag + + +@python_2_unicode_compatible +class Bookmark(models.Model): + url = models.URLField() + tags = GenericRelation(TaggedItem) + + def __str__(self): + return self.url diff --git a/tests/admin_filters/tests.py b/tests/admin_filters/tests.py index dcd4236776..2b9836f5d1 100644 --- a/tests/admin_filters/tests.py +++ b/tests/admin_filters/tests.py @@ -12,7 +12,7 @@ from django.test import TestCase, RequestFactory, override_settings from django.utils.encoding import force_text from django.utils import six -from .models import Book, Department, Employee +from .models import Book, Department, Employee, Bookmark, TaggedItem def select_by(dictlist, key, value): @@ -205,6 +205,10 @@ class DepartmentFilterDynamicValueBookAdmin(EmployeeAdmin): list_filter = [DepartmentListFilterLookupWithDynamicValue, ] +class BookmarkAdminGenericRelation(ModelAdmin): + list_filter = ['tags__tag'] + + class ListFiltersTests(TestCase): def setUp(self): @@ -539,6 +543,24 @@ class ListFiltersTests(TestCase): expected = [(self.bob.pk, 'bob'), (self.lisa.pk, 'lisa')] self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected)) + def test_listfilter_genericrelation(self): + django_bookmark = Bookmark.objects.create(url='https://www.djangoproject.com/') + python_bookmark = Bookmark.objects.create(url='https://www.python.org/') + kernel_bookmark = Bookmark.objects.create(url='https://www.kernel.org/') + + TaggedItem.objects.create(content_object=django_bookmark, tag='python') + TaggedItem.objects.create(content_object=python_bookmark, tag='python') + TaggedItem.objects.create(content_object=kernel_bookmark, tag='linux') + + modeladmin = BookmarkAdminGenericRelation(Bookmark, site) + + request = self.request_factory.get('/', {'tags__tag': 'python'}) + changelist = self.get_changelist(request, Bookmark, modeladmin) + queryset = changelist.get_queryset(request) + + expected = [python_bookmark, django_bookmark] + self.assertEqual(list(queryset), expected) + def test_booleanfieldlistfilter(self): modeladmin = BookAdmin(Book, site) self.verify_booleanfieldlistfilter(modeladmin) |
