Make .extra() only disable polymorphic query if an arg that is currently
not handled is actually used. I.e. if all args are supported, then do polymorphic query by default.
This commit is contained in:
committed by
Bert Constantin
parent
b2357592cb
commit
486a579ac7
+12
-5
@@ -85,11 +85,18 @@ class PolymorphicQuerySet(QuerySet):
|
||||
return super(PolymorphicQuerySet, self).aggregate(*args, **kwargs)
|
||||
|
||||
def extra(self, *args, **kwargs):
|
||||
"""only return polymorphic results if we get "polymorphic=True" as a keyword arg."""
|
||||
#for key in kwargs.keys():
|
||||
# if key not in ['where','order_by', 'params']:
|
||||
# assert False,"""django_polymorphic: extras() does not support keyword argument %s.
|
||||
self.polymorphic_disabled = not bool(kwargs.pop('polymorphic', False))
|
||||
#
|
||||
# Disable polymorphic, but only if arg that is currently not handled
|
||||
# polymorphically is passed in.
|
||||
#
|
||||
# Allow this to be overridden with 'polymorphic' arg
|
||||
#
|
||||
polymorphic_by_default = True
|
||||
for key in kwargs.keys():
|
||||
if key not in ['where','order_by', 'polymorphic']: # FIXME: also add 'params' here?
|
||||
polymorphic_by_default = False
|
||||
|
||||
self.polymorphic_disabled = not bool(kwargs.pop('polymorphic', polymorphic_by_default))
|
||||
return super(PolymorphicQuerySet, self).extra(*args, **kwargs)
|
||||
|
||||
def get_real_instances(self, base_result_objects):
|
||||
|
||||
@@ -357,10 +357,14 @@ __test__ = {"doctest": """
|
||||
### extra() method
|
||||
|
||||
>>> Model2A.objects.extra(where=['id IN (2, 3)'])
|
||||
[ <Model2B: id 2, field1 (CharField), field2 (CharField)>,
|
||||
<Model2C: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ]
|
||||
|
||||
>>> Model2A.objects.extra(where=['id IN (2, 3)'], select={'dummy_attribute':'1'})
|
||||
[ <Model2A: id 2, field1 (CharField)>,
|
||||
<Model2A: id 3, field1 (CharField)> ]
|
||||
|
||||
>>> Model2A.objects.extra(polymorphic=True, where=['id IN (2, 3)'])
|
||||
>>> Model2A.objects.extra(polymorphic=True, where=['id IN (2, 3)'], select={'dummy_attribute':'1'})
|
||||
[ <Model2B: id 2, field1 (CharField), field2 (CharField)>,
|
||||
<Model2C: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user