Minor cleanup of Lookup API

This commit is contained in:
Anssi Kääriäinen 2013-12-01 02:22:30 +02:00
parent 2adf50428d
commit eac4776684
2 changed files with 5 additions and 21 deletions

View File

@ -1,6 +1,5 @@
from copy import copy from copy import copy
from django.core.exceptions import FieldError
from django.conf import settings from django.conf import settings
from django.utils import timezone from django.utils import timezone
from django.utils.functional import cached_property from django.utils.functional import cached_property
@ -29,18 +28,10 @@ class Extract(object):
class Lookup(object): class Lookup(object):
lookup_name = None lookup_name = None
extract_class = None
def __init__(self, lhs, rhs): def __init__(self, lhs, rhs):
self.lhs, self.rhs = lhs, rhs self.lhs, self.rhs = lhs, rhs
if rhs is None: self.rhs = self.get_prep_lookup()
if not self.extract_class:
raise FieldError("Lookup '%s' doesn't support nesting." % self.lookup_name)
else:
self.rhs = self.get_prep_lookup()
def get_extract(self):
return self.extract_class(self.lhs)
def get_prep_lookup(self): def get_prep_lookup(self):
return self.lhs.output_type.get_prep_lookup(self.lookup_name, self.rhs) return self.lhs.output_type.get_prep_lookup(self.lookup_name, self.rhs)

View File

@ -93,16 +93,9 @@ The process_rhs method is used to convert the right hand side into query string.
The rhs is the value given in the filter clause. It can be a raw value to The rhs is the value given in the filter clause. It can be a raw value to
compare agains, a F() reference to another field or even a QuerySet. compare agains, a F() reference to another field or even a QuerySet.
.. method:: get_extract() In addition the Lookup class has some private methods - that is, implementing
just the above mentioned attributes and methods is not enough, instead you
The get_extract method is used in nested lookups. It must return an Extract instance. should subclass Lookup.
.. classattribute:: Lookup.extract_class
The default implementation of get_extract() will return an instance of extract_class.
In addition there are some private methods - that is, implementing just the above
mentioned attributes and methods is not enough, you must subclass Lookup instead.
The Extract class The Extract class
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
@ -112,7 +105,7 @@ For example you could have an Extract that procudes modulo 3 of the given value.
In SQL this would be something like "author"."age" % 3. In SQL this would be something like "author"."age" % 3.
Extracts are used in nested lookups. The Extract class must implement the query Extracts are used in nested lookups. The Extract class must implement the query
part interface. part interface. In addition the Extract should must lookup_name attribute.
A simple Lookup example A simple Lookup example
~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~