mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
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:
parent
afe99ddd18
commit
5e8ebef5b1
1 changed files with 10 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue