mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 04:10:24 +00:00
Merge pull request #101 from funkybob/issue-34
Attempt to carry over extras(select) values from parent
This commit is contained in:
commit
febc3d645a
4 changed files with 15 additions and 0 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
------------------
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ class InheritanceQuerySetMixin(object):
|
|||
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)
|
||||
|
|
@ -86,6 +87,9 @@ class InheritanceQuerySetMixin(object):
|
|||
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:
|
||||
|
|
|
|||
|
|
@ -974,6 +974,14 @@ class InheritanceManagerUsingModelsTests(TestCase):
|
|||
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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue