non_polymorphic() queryset member function added
parent
19adbdaf2c
commit
8c3df56cb6
12
DOCS.rst
12
DOCS.rst
|
|
@ -235,15 +235,15 @@ existing polymorphic inheritance tree::
|
|||
Non-Polymorphic Queries
|
||||
-----------------------
|
||||
|
||||
>>> ModelA.base_objects.all()
|
||||
>>> ModelA.objects.all().non_polymorphic()
|
||||
.
|
||||
[ <ModelA: id 1, field1 (CharField)>,
|
||||
<ModelA: id 2, field1 (CharField)>,
|
||||
<ModelA: id 3, field1 (CharField)> ]
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
|
|
@ -289,6 +289,13 @@ __test__ = {"doctest": """
|
|||
>>> o.get_real_instance()
|
||||
<Model2C: id 3, field1 (CharField), field2 (CharField), field3 (CharField)>
|
||||
|
||||
# non_polymorphic()
|
||||
>>> Model2A.objects.all().non_polymorphic()
|
||||
[ <Model2A: id 1, field1 (CharField)>,
|
||||
<Model2A: id 2, field1 (CharField)>,
|
||||
<Model2A: id 3, field1 (CharField)>,
|
||||
<Model2A: id 4, field1 (CharField)> ]
|
||||
|
||||
|
||||
### test inheritance pointers & _base_managers
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue