diff options
author | Marc Tamlyn <marc.tamlyn@gmail.com> | 2015-01-10 16:11:15 +0000 |
---|---|---|
committer | Marc Tamlyn <marc.tamlyn@gmail.com> | 2015-01-10 16:18:19 +0000 |
commit | 916e38802f151b34aaca487dc7e928946e81be73 (patch) | |
tree | 891bfce8727a3321b93850d9822250075dcb82e5 /django/contrib/postgres/lookups.py | |
parent | 74f02557e0183812d6d60e2548985c5c40b3d27b (diff) | |
download | django-916e38802f151b34aaca487dc7e928946e81be73.tar.gz |
Move % addition to lookups, refactor postgres lookups.
These refactorings making overriding some text based lookup names on
other fields (specifically `contains`) much cleaner. It also removes a
bunch of duplication in the contrib.postgres lookups.
Diffstat (limited to 'django/contrib/postgres/lookups.py')
-rw-r--r-- | django/contrib/postgres/lookups.py | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/django/contrib/postgres/lookups.py b/django/contrib/postgres/lookups.py index 4cf51dbd9c..eb7cfd8359 100644 --- a/django/contrib/postgres/lookups.py +++ b/django/contrib/postgres/lookups.py @@ -1,10 +1,36 @@ -from django.db.models import Transform +from django.db.models import Lookup, Transform -class Unaccent(Transform): +class PostgresSimpleLookup(Lookup): + def as_sql(self, qn, connection): + lhs, lhs_params = self.process_lhs(qn, connection) + rhs, rhs_params = self.process_rhs(qn, connection) + params = lhs_params + rhs_params + return '%s %s %s' % (lhs, self.operator, rhs), params + + +class FunctionTransform(Transform): + def as_sql(self, qn, connection): + lhs, params = qn.compile(self.lhs) + return "%s(%s)" % (self.function, lhs), params + + +class DataContains(PostgresSimpleLookup): + lookup_name = 'contains' + operator = '@>' + + +class ContainedBy(PostgresSimpleLookup): + lookup_name = 'contained_by' + operator = '<@' + + +class Overlap(PostgresSimpleLookup): + lookup_name = 'overlap' + operator = '&&' + + +class Unaccent(FunctionTransform): bilateral = True lookup_name = 'unaccent' - - def as_postgresql(self, compiler, connection): - lhs, params = compiler.compile(self.lhs) - return "UNACCENT(%s)" % lhs, params + function = 'UNACCENT' |