diff --git a/DOCS.rst b/DOCS.rst index 4c921e4..e299610 100644 --- a/DOCS.rst +++ b/DOCS.rst @@ -234,16 +234,16 @@ existing polymorphic inheritance tree:: Non-Polymorphic Queries ----------------------- - ->>> ModelA.base_objects.all() + +>>> ModelA.objects.all().non_polymorphic() . [ , , ] -Each polymorphic model has 'base_objects' defined as a normal -Django manager. Of course, arbitrary custom managers may be -added to the models as well. +Except for the return of the of the base class objects, there are no +changes in the behaviour of the queryset (i.e. the enhancements +for ``filter()`` or ``instance_of()`` etc. still work as expected). About Queryset Methods ---------------------- @@ -270,8 +270,8 @@ About Queryset Methods + ``get_real_instances(base_objects_list_or_queryset)`` allows you to turn a queryset or list of base model objects efficiently into the real objects. - For example, you could do ``base_objects=ModelA.base_objects.extra(...)`` and - then call ``real_objects=ModelA.objects.get_real_instances(base_objects)``. + For example, you could do ``base_objects=ModelA.extra(...).non_polymorphic()`` + and then call ``real_objects=ModelA.objects.get_real_instances(base_objects)``. * ``values()`` & ``values_list()`` currently do not return polymorphic results. This may change in the future however. If you want to use these diff --git a/polymorphic/query.py b/polymorphic/query.py index 5d1e525..5235608 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -42,6 +42,13 @@ class PolymorphicQuerySet(QuerySet): new.polymorphic_disabled = self.polymorphic_disabled return new + def non_polymorphic(self, *args, **kwargs): + """switch off polymorphic behaviour for this query. + When the queryset is evaluated, only objects of the type of the + base class used for this query are returned.""" + self.polymorphic_disabled = True + return self + def instance_of(self, *args): """Filter the queryset to only include the classes in args (and their subclasses). Implementation in _translate_polymorphic_filter_defnition.""" diff --git a/polymorphic/tests.py b/polymorphic/tests.py index 930d084..97ce12b 100644 --- a/polymorphic/tests.py +++ b/polymorphic/tests.py @@ -289,6 +289,13 @@ __test__ = {"doctest": """ >>> o.get_real_instance() +# non_polymorphic() +>>> Model2A.objects.all().non_polymorphic() +[ , + , + , + ] + ### test inheritance pointers & _base_managers