8000 [1.7.x] Fixed #23616 - Fixed generic relations in ModelAdmin.list_fil… · alex-python/django@fd3dccb · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit fd3dccb

Browse files
kswiattimgraham
authored andcommitted
[1.7.x] Fixed #23616 - Fixed generic relations in ModelAdmin.list_filter.
Thanks ranjur for reporting bug, timgraham for review, and collinanderson for contributing tips. Backport of 06b11b6 from master
1 parent 71988ed commit fd3dccb

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

django/contrib/admin/options.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,10 @@ def lookup_allowed(self, lookup, value):
413413
# since it's ignored in ChangeList.get_filters().
414414
return True
415415
model = field.rel.to
416-
rel_name = field.rel.get_related_field().name
416+
if hasattr(field.rel, 'get_related_field'):
417+
rel_name = field.rel.get_related_field().name
418+
else:
419+
rel_name = None
417420
elif isinstance(field, RelatedObject):
418421
model = field.model
419422
rel_name = model._meta.pk.name

docs/releases/1.7.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,5 @@ Bugfixes
112112
* Added a prompt to the migrations questioner when removing the null constraint
113113
from a field to prevent an IntegrityError on existing NULL rows
114114
(:ticket:`23609`).
115+
1 8000 16+
* Fixed generic relations in ``ModelAdmin.list_filter`` (:ticket:`23616`).

tests/admin_filters/models.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import unicode_literals
22

33
from django.contrib.auth.models import User
4+
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
5+
from django.contrib.contenttypes.models import ContentType
46
from django.db import models
57
from django.utils.encoding import python_2_unicode_compatible
68

@@ -35,3 +37,23 @@ class Employee(models.Model):
3537

3638
def __str__(self):
3739
return self.name
40+
41+
42+
@python_2_unicode_compatible
43+
class TaggedItem(models.Model):
44+
tag = models.SlugField()
45+
content_type = models.ForeignKey(ContentType, related_name='tagged_items')
46+
object_id = models.PositiveIntegerField()
47+
content_object = GenericForeignKey('content_type', 'object_id')
48+
49+
def __str__(self):
50+
return self.tag
51+
52+
53+
@python_2_unicode_compatible
54+
class Bookmark(models.Model):
55+
url = models.URLField()
56+
tags = GenericRelation(TaggedItem)
57+
58+
def __str__(self):
59+
return self.url

tests/admin_filters/tests.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from django.utils.encoding import force_text
1313
from django.utils import six
1414

15-
from .models import Book, Department, Employee
15+
from .models import Book, Department, Employee, Bookmark, TaggedItem
1616

1717

1818
def select_by(dictlist, key, value):
@@ -184,6 +184,10 @@ class DepartmentFilterDynamicValueBookAdmin(EmployeeAdmin):
184184
list_filter = [DepartmentListFilterLookupWithDynamicValue, ]
185185

186186

187+
class BookmarkAdminGenericRelation(ModelAdmin):
188+
list_filter = ['tags__tag']
189+
190+
187191
class ListFiltersTests(TestCase):
188192

189193
def setUp(self):
@@ -464,6 +468,24 @@ def test_relatedfieldlistfilter_reverse_relationships(self):
464468
self.assertEqual(choice['selected'], True)
465469
self.assertEqual(choice['query_string'], '?books_contributed__id__exact=%d' % self.django_book.pk)
466470

471+
def test_listfilter_genericrelation(self):
472+
django_bookmark = Bookmark.objects.create(url='https://www.djangoproject.com/')
473+
python_bookmark = Bookmark.objects.create(url='https://www.python.org/')
474+
kernel_bookmark = Bookmark.objects.create(url='https://www.kernel.org/')
475+
476+
TaggedItem.objects.create(content_object=django_bookmark, tag='python')
477+
TaggedItem.objects.create(content_object=python_bookmark, tag='python')
478+
TaggedItem.objects.create(content_object=kernel_bookmark, tag='linux')
479+
480+
modeladmin = BookmarkAdminGenericRelation(Bookmark, site)
481+
482+
request = self.request_factory.get('/', {'tags__tag': 'python'})
483+
changelist = self.get_changelist(request, Bookmark, modeladmin)
484+
queryset = changelist.get_queryset(request)
485+
486+
expected = [python_bookmark, django_bookmark]
487+
self.assertEqual(list(queryset), expected)
488+
467489
def test_booleanfieldlistfilter(self):
468490
modeladmin = BookAdmin(Book, site)
469491
self.verify_booleanfieldlistfilter(modeladmin)

0 commit comments

Comments
 (0)
0