mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 04:10:24 +00:00
Okay, now we are getting somewhere.
By itself, .instance_of(*models) will actually call select_subclasses(*models), which results in just those objects being cast to subclasses. However, if you want the casting to grandchildren (which is supported only in django 1.6+), then you may use an extra call to .select_subclasses() (without arguments, to select all subclasses). It doesn't deal with @kezabelle's discussion of mixed-use of select_subclasses/ instance_of, I still need to look at that. But it does pass all current tests...
This commit is contained in:
parent
f54c9a7c95
commit
02e19e45b4
2 changed files with 33 additions and 9 deletions
|
|
@ -171,6 +171,9 @@ class InheritanceQuerySetMixin(object):
|
|||
return levels
|
||||
|
||||
def instance_of(self, *models):
|
||||
"""
|
||||
Fetch only objects that are instances of the provided model(s).
|
||||
"""
|
||||
# If we aren't already selecting the subclasess, we need
|
||||
# to in order to get this to work.
|
||||
|
||||
|
|
@ -190,7 +193,7 @@ class InheritanceQuerySetMixin(object):
|
|||
) for field in model._meta.parents.values()
|
||||
]) + ')')
|
||||
|
||||
return self.extra(where=[' OR '.join(where_queries)])
|
||||
return self.select_subclasses(*models).extra(where=[' OR '.join(where_queries)])
|
||||
|
||||
class InheritanceManagerMixin(object):
|
||||
use_for_related_fields = True
|
||||
|
|
@ -206,8 +209,8 @@ class InheritanceManagerMixin(object):
|
|||
def get_subclass(self, *args, **kwargs):
|
||||
return self.get_queryset().get_subclass(*args, **kwargs)
|
||||
|
||||
def instance_of(self, model):
|
||||
return self.get_queryset().instance_of(model)
|
||||
def instance_of(self, *models):
|
||||
return self.get_queryset().instance_of(*models)
|
||||
|
||||
class InheritanceQuerySet(InheritanceQuerySetMixin, QuerySet):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -992,30 +992,51 @@ class InheritanceManagerUsingModelsTests(TestCase):
|
|||
|
||||
def test_limit_to_specific_subclass(self):
|
||||
child3 = InheritanceManagerTestChild3.objects.create()
|
||||
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestChild3)
|
||||
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3)
|
||||
|
||||
self.assertEqual([child3], list(results))
|
||||
|
||||
@skipUnless(django.VERSION >= (1, 6, 0), "test only applies to Django 1.6+")
|
||||
def test_limit_to_specific_grandchild_class(self):
|
||||
grandchild1 = InheritanceManagerTestGrandChild1.objects.get()
|
||||
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestGrandChild1)
|
||||
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestGrandChild1)
|
||||
|
||||
self.assertEqual([grandchild1], list(results))
|
||||
|
||||
def test_limit_to_child_fetches_grandchildren(self):
|
||||
children = InheritanceManagerTestChild1.objects.select_subclasses().all()
|
||||
def test_limit_to_child_fetches_grandchildren_as_child_class(self):
|
||||
# Not sure if this is the desired behaviour...?
|
||||
children = InheritanceManagerTestChild1.objects.all()
|
||||
|
||||
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestChild1)
|
||||
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild1)
|
||||
|
||||
self.assertEqual(set(children), set(results))
|
||||
|
||||
@skipUnless(django.VERSION >= (1, 6, 0), "test only applies to Django 1.6+")
|
||||
def test_can_fetch_limited_class_grandchildren(self):
|
||||
# Not sure if this is the desired behaviour...?
|
||||
children = InheritanceManagerTestChild1.objects.select_subclasses()
|
||||
|
||||
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild1).select_subclasses()
|
||||
|
||||
self.assertEqual(set(children), set(results))
|
||||
|
||||
def test_selecting_multiple_instance_classes(self):
|
||||
child3 = InheritanceManagerTestChild3.objects.create()
|
||||
children1 = InheritanceManagerTestChild1.objects.all()
|
||||
|
||||
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3, InheritanceManagerTestChild1)
|
||||
|
||||
self.assertEqual(set([child3] + list(children1)), set(results))
|
||||
|
||||
@skipUnless(django.VERSION >= (1, 6, 0), "test only applies to Django 1.6+")
|
||||
def test_selecting_multiple_instance_classes_including_grandchildren(self):
|
||||
child3 = InheritanceManagerTestChild3.objects.create()
|
||||
grandchild1 = InheritanceManagerTestGrandChild1.objects.get()
|
||||
|
||||
results = InheritanceManagerTestParent.objects.select_subclasses().instance_of(InheritanceManagerTestChild3, InheritanceManagerTestGrandChild1)
|
||||
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3, InheritanceManagerTestGrandChild1).select_subclasses()
|
||||
|
||||
self.assertEqual(set([child3, grandchild1]), set(results))
|
||||
|
||||
|
||||
class InheritanceManagerRelatedTests(InheritanceManagerTests):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue