Attempt to carry over extras(select) values from parent

This commit is contained in:
Curtis 2014-01-08 15:12:55 +11:00
parent 87f7209042
commit 6f30e88ba5
2 changed files with 13 additions and 0 deletions

View file

@ -70,6 +70,7 @@ class InheritanceQuerySet(QuerySet):
def iterator(self):
iter = super(InheritanceQuerySet, self).iterator()
if getattr(self, 'subclasses', False):
extras = self.query.extra_select.keys()
# sort the subclass names longest first,
# so with 'a' and 'a__b' it goes as deep as possible
subclasses = sorted(self.subclasses, key=len, reverse=True)
@ -86,6 +87,9 @@ class InheritanceQuerySet(QuerySet):
for k in self._annotated:
setattr(sub_obj, k, getattr(obj, k))
for k in extras:
setattr(sub_obj, k, getattr(obj, k))
yield sub_obj
else:
for obj in iter:

View file

@ -974,6 +974,15 @@ class InheritanceManagerUsingModelsTests(TestCase):
self.assertEqual(set(results.subclasses),
set(expected_related_names))
def test_extras_descend(self):
"""
Ensure that if extras(select=) is passed, we copy the values down onto
sub classes.
"""
results = InheritanceManagerTestParent.objects.all().extra(
select={'foo': 'id + 1'}
)
self.assertTrue(all(result.foo == (result.id + 1) for result in results))
class InheritanceManagerRelatedTests(InheritanceManagerTests):