non_polymorphic() queryset member function added

fix_request_path_info
Bert Constantin 2010-10-23 12:20:23 +02:00
parent 19adbdaf2c
commit 8c3df56cb6
3 changed files with 21 additions and 7 deletions

View File

@ -234,16 +234,16 @@ existing polymorphic inheritance tree::
Non-Polymorphic Queries Non-Polymorphic Queries
----------------------- -----------------------
>>> ModelA.base_objects.all() >>> ModelA.objects.all().non_polymorphic()
. .
[ <ModelA: id 1, field1 (CharField)>, [ <ModelA: id 1, field1 (CharField)>,
<ModelA: id 2, field1 (CharField)>, <ModelA: id 2, field1 (CharField)>,
<ModelA: id 3, field1 (CharField)> ] <ModelA: id 3, field1 (CharField)> ]
Each polymorphic model has 'base_objects' defined as a normal Except for the return of the of the base class objects, there are no
Django manager. Of course, arbitrary custom managers may be changes in the behaviour of the queryset (i.e. the enhancements
added to the models as well. for ``filter()`` or ``instance_of()`` etc. still work as expected).
About Queryset Methods About Queryset Methods
---------------------- ----------------------
@ -270,8 +270,8 @@ About Queryset Methods
+ ``get_real_instances(base_objects_list_or_queryset)`` allows you to turn a + ``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. queryset or list of base model objects efficiently into the real objects.
For example, you could do ``base_objects=ModelA.base_objects.extra(...)`` and For example, you could do ``base_objects=ModelA.extra(...).non_polymorphic()``
then call ``real_objects=ModelA.objects.get_real_instances(base_objects)``. and then call ``real_objects=ModelA.objects.get_real_instances(base_objects)``.
* ``values()`` & ``values_list()`` currently do not return polymorphic * ``values()`` & ``values_list()`` currently do not return polymorphic
results. This may change in the future however. If you want to use these results. This may change in the future however. If you want to use these

View File

@ -42,6 +42,13 @@ class PolymorphicQuerySet(QuerySet):
new.polymorphic_disabled = self.polymorphic_disabled new.polymorphic_disabled = self.polymorphic_disabled
return new 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): def instance_of(self, *args):
"""Filter the queryset to only include the classes in args (and their subclasses). """Filter the queryset to only include the classes in args (and their subclasses).
Implementation in _translate_polymorphic_filter_defnition.""" Implementation in _translate_polymorphic_filter_defnition."""

View File

@ -289,6 +289,13 @@ __test__ = {"doctest": """
>>> o.get_real_instance() >>> o.get_real_instance()
<Model2C: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> <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 ### test inheritance pointers & _base_managers