Add ability to filter an InheritanceQuerySet by model.

This is based upon the feature in django-polymorphic, where you
can do:

    SuperClass.objects.instance_of(SubClass)

This will result in only objects of the subclass being fetched.

Note: this works with any queryset, keeping the filtering and
ordering applied there.
This commit is contained in:
Matthew Schinckel 2014-05-16 20:23:04 +09:30
parent afe99ddd18
commit 5e8ebef5b1

View file

@ -170,6 +170,14 @@ class InheritanceQuerySetMixin(object):
levels = 1
return levels
def instance_of(self, model):
parent_field = model._meta.parents.values()[0].attname
query = '"%s"."%s" IS NOT NULL' % (
model._meta.db_table,
parent_field
)
return self.extra(where=[query])
class InheritanceManagerMixin(object):
use_for_related_fields = True
@ -184,6 +192,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)
class InheritanceQuerySet(InheritanceQuerySetMixin, QuerySet):
pass