WIP: this fails on django < 1.6

I think this is actually related to the issue related in #23.
This commit is contained in:
Matthew Schinckel 2014-05-20 20:58:25 +09:30
parent f06b573772
commit 4128524eb4
2 changed files with 36 additions and 2 deletions

View file

@ -170,7 +170,22 @@ class InheritanceQuerySetMixin(object):
levels = 1
return levels
def instance_of(self, model):
def instance_of(self, *models):
# If we aren't already selecting the subclasess, we need
# to in order to get this to work.
where_queries = []
for model in models:
where_queries.append('(' + ' AND '.join([
'"%s"."%s" IS NOT NULL' % (
model._meta.db_table,
field.attname
) for field in model._meta.parents.values()
]) + ')')
return self.extra(where=[' OR '.join(where_queries)])
# We need to get the
parent_field = model._meta.parents.values()[0].attname
query = '"%s"."%s" IS NOT NULL' % (
model._meta.db_table,

View file

@ -994,9 +994,28 @@ class InheritanceManagerUsingModelsTests(TestCase):
child3 = InheritanceManagerTestChild3.objects.create()
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestChild3)
self.assertEqual(1, len(results))
self.assertEqual([child3], list(results))
def test_limit_to_specific_grandchild_class(self):
grandchild1 = InheritanceManagerTestGrandChild1.objects.get()
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestGrandChild1)
self.assertEqual([grandchild1], list(results))
def test_limit_to_child_fetches_grandchildren(self):
children = InheritanceManagerTestChild1.objects.select_subclasses().all()
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestChild1)
self.assertEqual(set(children), set(results))
def test_selecting_multiple_instance_classes(self):
child3 = InheritanceManagerTestChild3.objects.create()
grandchild1 = InheritanceManagerTestGrandChild1.objects.get()
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestChild3, InheritanceManagerTestGrandChild1)
self.assertEqual(set([child3, grandchild1]), set(results))
class InheritanceManagerRelatedTests(InheritanceManagerTests):
def setUp(self):