From 4252a14c396bb806a86dfda2d0f6a4f3d96c102a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Sat, 15 Nov 2014 13:04:02 +0200 Subject: [PATCH] Moved bilateral transform fetching to models.lookups --- django/db/models/lookups.py | 15 +++++++++++++-- django/db/models/sql/query.py | 11 ++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index af966723f6b..062a9a10edb 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -78,14 +78,25 @@ class Transform(RegisterLookupMixin): def get_group_by_cols(self): return self.lhs.get_group_by_cols() + def get_bilateral_transforms(self): + if hasattr(self.lhs, 'get_bilateral_transforms'): + bilateral_transforms = self.lhs.get_bilateral_transforms() + else: + bilateral_transforms = [] + if self.bilateral: + bilateral_transforms.append((self.__class__, self.init_lookups)) + return bilateral_transforms + class Lookup(RegisterLookupMixin): lookup_name = None - def __init__(self, lhs, rhs, bilateral_transforms=None): + def __init__(self, lhs, rhs): self.lhs, self.rhs = lhs, rhs self.rhs = self.get_prep_lookup() - if bilateral_transforms is None: + if hasattr(self.lhs, 'get_bilateral_transforms'): + bilateral_transforms = self.lhs.get_bilateral_transforms() + else: bilateral_transforms = [] if bilateral_transforms: # We should warn the user as soon as possible if he is trying to apply diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index a17cd62f298..ce932ddffb1 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1077,7 +1077,6 @@ class Query(object): and get_transform(). """ lookups = lookups[:] - bilaterals = [] while lookups: name = lookups[0] # If there is just one part left, try first get_lookup() so @@ -1089,21 +1088,19 @@ class Query(object): # We didn't find a lookup. We are going to interpret # the name as transform, and do an Exact lookup against # it. - lhs = self.try_transform(lhs, name, lookups, bilaterals) + lhs = self.try_transform(lhs, name, lookups) final_lookup = lhs.get_lookup('exact') - return final_lookup(lhs, rhs, bilaterals) - lhs = self.try_transform(lhs, name, lookups, bilaterals) + return final_lookup(lhs, rhs) + lhs = self.try_transform(lhs, name, lookups) lookups = lookups[1:] - def try_transform(self, lhs, name, rest_of_lookups, bilaterals): + def try_transform(self, lhs, name, rest_of_lookups): """ Helper method for build_lookup. Tries to fetch and initialize a transform for name parameter from lhs. """ next = lhs.get_transform(name) if next: - if getattr(next, 'bilateral', False): - bilaterals.append((next, rest_of_lookups)) return next(lhs, rest_of_lookups) else: raise FieldError(