Ticket #24169: array.py-patch

File array.py-patch, 1.7 KB (added by Joel Burton, 9 years ago)

Patch, along with tests

Line 
1diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py
2index b0850b92..af575c6b 100644
3--- a/django/contrib/postgres/fields/array.py
4+++ b/django/contrib/postgres/fields/array.py
5@@ -158,8 +158,20 @@ class ArrayContains(lookups.DataContains):
6 return sql, params
7
8
9-ArrayField.register_lookup(lookups.ContainedBy)
10-ArrayField.register_lookup(lookups.Overlap)
11+@ArrayField.register_lookup
12+class ArrayContainedBy(lookups.ContainedBy):
13+ def as_sql(self, qn, connection):
14+ sql, params = super(ArrayContainedBy, self).as_sql(qn, connection)
15+ sql += '::%s' % self.lhs.output_field.db_type(connection)
16+ return sql, params
17+
18+
19+@ArrayField.register_lookup
20+class ArrayOverlap(lookups.Overlap):
21+ def as_sql(self, qn, connection):
22+ sql, params = super(ArrayOverlap, self).as_sql(qn, connection)
23+ sql += '::%s' % self.lhs.output_field.db_type(connection)
24+ return sql, params
25
26
27 @ArrayField.register_lookup
28diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py
29index 5c300f7e..020b3165 100644
30--- a/tests/postgres_tests/test_array.py
31+++ b/tests/postgres_tests/test_array.py
32@@ -156,6 +156,18 @@ class TestQuerying(TestCase):
33 []
34 )
35
36+ def test_contained_by_charfield(self):
37+ self.assertSequenceEqual(
38+ CharArrayModel.objects.filter(field__contained_by=['text']),
39+ []
40+ )
41+
42+ def test_overlap_charfield(self):
43+ self.assertSequenceEqual(
44+ CharArrayModel.objects.filter(field__overlap=['text']),
45+ []
46+ )
47+
48 def test_index(self):
49 self.assertSequenceEqual(
50 NullableIntegerArrayModel.objects.filter(field__0=2),
Back to Top