summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/lookups.py
diff options
context:
space:
mode:
authorMarc Tamlyn <marc.tamlyn@gmail.com>2015-01-10 16:11:15 +0000
committerMarc Tamlyn <marc.tamlyn@gmail.com>2015-01-10 16:18:19 +0000
commit916e38802f151b34aaca487dc7e928946e81be73 (patch)
tree891bfce8727a3321b93850d9822250075dcb82e5 /django/contrib/postgres/lookups.py
parent74f02557e0183812d6d60e2548985c5c40b3d27b (diff)
downloaddjango-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.py38
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'