From a175b4d0fb570b4ac14c680cf27a76534c76adf2 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 19 Aug 2014 14:27:56 +0100 Subject: [PATCH] Improvements to type() PageQuerySet method This commit makes the type PageQuerySet method include all concrete models that descend from the specified model in the filter. This makes this method useful for cases where you have many page types descending from a single abstract model and want to search across all these page types (which you can't do in Django normally). --- wagtail/wagtailcore/query.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/wagtail/wagtailcore/query.py b/wagtail/wagtailcore/query.py index 81d78dc37..e5eb341d1 100644 --- a/wagtail/wagtailcore/query.py +++ b/wagtail/wagtailcore/query.py @@ -1,4 +1,4 @@ -from django.db.models import Q +from django.db.models import Q, get_models from django.contrib.contenttypes.models import ContentType from treebeard.mp_tree import MP_NodeQuerySet @@ -152,9 +152,13 @@ class PageQuerySet(MP_NodeQuerySet): """ return self.exclude(self.sibling_of_q(other, inclusive)) - def type_q(self, model): - content_type = ContentType.objects.get_for_model(model) - return Q(content_type=content_type) + def type_q(self, klass): + content_types = ContentType.objects.get_for_models(*[ + model for model in get_models() + if issubclass(model, klass) + ]).values() + + return Q(content_type__in=content_types) def type(self, model): """