Skip to content

Commit

Permalink
Merge 5b6a3ff into 6dc8c94
Browse files Browse the repository at this point in the history
  • Loading branch information
funkybob committed Jan 21, 2014
2 parents 6dc8c94 + 5b6a3ff commit 9b1983c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Travis Swicegood <travis@domain51.com>
Trey Hunner <trey@treyhunner.com>
zyegfryed
Filipe Ximenes <filipeximenes@gmail.com>
Curtis Maloney <curtis@tinbrain.net>
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ master (unreleased)
* MonitorField now accepts a 'when' parameter. It will update only when the field
changes to one of the values specified.

* Improve `InheritanceManager` so any attributes added by using extra(select)
will be propagated onto children.

1.5.0 (2013.08.29)
------------------
Expand Down
4 changes: 4 additions & 0 deletions model_utils/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def annotate(self, *args, **kwargs):
def iterator(self):
iter = super(InheritanceQuerySetMixin, self).iterator()
if getattr(self, 'subclasses', False):
extras = tuple(self.query.extra.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)
Expand All @@ -86,6 +87,9 @@ def iterator(self):
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:
Expand Down
8 changes: 8 additions & 0 deletions model_utils/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,14 @@ def test_manually_specifying_parent_fk_only_specific_child(self):
self.assertEqual(set(results.subclasses),
set(expected_related_names))

def test_extras_descend(self):
"""
Ensure that extra(select=) values are copied onto sub-classes.
"""
results = InheritanceManagerTestParent.objects.select_subclasses().extra(
select={'foo': 'id + 1'}
)
self.assertTrue(all(result.foo == (result.id + 1) for result in results))


class InheritanceManagerRelatedTests(InheritanceManagerTests):
Expand Down

0 comments on commit 9b1983c

Please sign in to comment.