From 08232ef84d4959826ad5136f183c9fc5bedf0599 Mon Sep 17 00:00:00 2001 From: Andriy Sokolovskiy Date: Thu, 4 Jun 2015 15:51:07 +0100 Subject: [PATCH] Fixed #24744 - Fixed relabeled_clone for the Transform --- django/db/models/lookups.py | 7 ++++++- docs/releases/1.8.3.txt | 2 ++ tests/custom_lookups/tests.py | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 45974093b48..9bcf8d63039 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -70,8 +70,13 @@ class Transform(RegisterLookupMixin): def output_field(self): return self.lhs.output_field + def copy(self): + return copy(self) + def relabeled_clone(self, relabels): - return self.__class__(self.lhs.relabeled_clone(relabels)) + copy = self.copy() + copy.lhs = self.lhs.relabeled_clone(relabels) + return copy def get_group_by_cols(self): return self.lhs.get_group_by_cols() diff --git a/docs/releases/1.8.3.txt b/docs/releases/1.8.3.txt index 6ac0d3f5923..dc5edbcb9e2 100644 --- a/docs/releases/1.8.3.txt +++ b/docs/releases/1.8.3.txt @@ -57,3 +57,5 @@ Bugfixes * Corrected join promotion for multiple ``Case`` expressions. Annotating a query with multiple ``Case`` expressions could unexpectedly filter out results (:ticket:`24924`). + +* Fixed usage of transforms in subqueries (:ticket:`24744`). diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py index 1b069047e40..dd8d431d3da 100644 --- a/tests/custom_lookups/tests.py +++ b/tests/custom_lookups/tests.py @@ -559,3 +559,18 @@ class CustomisedMethodsTests(TestCase): def test_overridden_get_transform_chain(self): q = CustomModel.objects.filter(field__transformfunc_banana__transformfunc_pear=3) self.assertIn('pear()', str(q.query)) + + +class SubqueryTransformTests(TestCase): + def test_subquery_usage(self): + models.IntegerField.register_lookup(Div3Transform) + try: + Author.objects.create(name='a1', age=1) + a2 = Author.objects.create(name='a2', age=2) + Author.objects.create(name='a3', age=3) + Author.objects.create(name='a4', age=4) + self.assertQuerysetEqual( + Author.objects.order_by('name').filter(id__in=Author.objects.filter(age__div3=2)), + [a2], lambda x: x) + finally: + models.IntegerField._unregister_lookup(Div3Transform)