mirror of https://github.com/django/django.git
[1.11.x] Fixed #28702 -- Made query lookups for CIText fields use citext.
Backport of f0a68c2511
from master
This commit is contained in:
parent
899999db42
commit
3545e84488
|
@ -3,7 +3,11 @@ from django.db.models import CharField, EmailField, TextField
|
|||
__all__ = ['CICharField', 'CIEmailField', 'CIText', 'CITextField']
|
||||
|
||||
|
||||
class CIText:
|
||||
class CIText(object):
|
||||
|
||||
def get_internal_type(self):
|
||||
return 'CI' + super(CIText, self).get_internal_type()
|
||||
|
||||
def db_type(self, connection):
|
||||
return 'citext'
|
||||
|
||||
|
|
|
@ -83,6 +83,8 @@ class DatabaseOperations(BaseDatabaseOperations):
|
|||
'istartswith', 'endswith', 'iendswith', 'regex', 'iregex'):
|
||||
if internal_type in ('IPAddressField', 'GenericIPAddressField'):
|
||||
lookup = "HOST(%s)"
|
||||
elif internal_type in ('CICharField', 'CIEmailField', 'CITextField'):
|
||||
lookup = '%s::citext'
|
||||
else:
|
||||
lookup = "%s::text"
|
||||
|
||||
|
|
|
@ -24,3 +24,6 @@ Bugfixes
|
|||
|
||||
* Fixed crash on SQLite and MySQL when ordering by a filtered subquery that
|
||||
uses ``nulls_first`` or ``nulls_last`` (:ticket:`28848`).
|
||||
|
||||
* Made query lookups for ``CICharField``, ``CIEmailField``, and ``CITextField``
|
||||
use a ``citext`` cast (:ticket:`28702`).
|
||||
|
|
|
@ -377,9 +377,15 @@ class PostgreSQLTests(TestCase):
|
|||
from django.db.backends.postgresql.operations import DatabaseOperations
|
||||
|
||||
do = DatabaseOperations(connection=None)
|
||||
for lookup in ('iexact', 'contains', 'icontains', 'startswith',
|
||||
'istartswith', 'endswith', 'iendswith', 'regex', 'iregex'):
|
||||
lookups = (
|
||||
'iexact', 'contains', 'icontains', 'startswith', 'istartswith',
|
||||
'endswith', 'iendswith', 'regex', 'iregex',
|
||||
)
|
||||
for lookup in lookups:
|
||||
self.assertIn('::text', do.lookup_cast(lookup))
|
||||
for lookup in lookups:
|
||||
for field_type in ('CICharField', 'CIEmailField', 'CITextField'):
|
||||
self.assertIn('::citext', do.lookup_cast(lookup, internal_type=field_type))
|
||||
|
||||
def test_correct_extraction_psycopg2_version(self):
|
||||
from django.db.backends.postgresql.base import psycopg2_version
|
||||
|
|
|
@ -12,6 +12,7 @@ from .models import CITestModel
|
|||
|
||||
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
|
||||
class CITextTestCase(PostgreSQLTestCase):
|
||||
case_sensitive_lookups = ('contains', 'startswith', 'endswith', 'regex')
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
|
@ -42,3 +43,18 @@ class CITextTestCase(PostgreSQLTestCase):
|
|||
instance = CITestModel.objects.get()
|
||||
self.assertEqual(instance.array_field, self.john.array_field)
|
||||
self.assertTrue(CITestModel.objects.filter(array_field__contains=['joe']).exists())
|
||||
|
||||
def test_lookups_name_char(self):
|
||||
for lookup in self.case_sensitive_lookups:
|
||||
query = {'name__{}'.format(lookup): 'john'}
|
||||
self.assertSequenceEqual(CITestModel.objects.filter(**query), [self.john])
|
||||
|
||||
def test_lookups_description_text(self):
|
||||
for lookup, string in zip(self.case_sensitive_lookups, ('average', 'average joe', 'john', 'Joe.named')):
|
||||
query = {'description__{}'.format(lookup): string}
|
||||
self.assertSequenceEqual(CITestModel.objects.filter(**query), [self.john])
|
||||
|
||||
def test_lookups_email(self):
|
||||
for lookup, string in zip(self.case_sensitive_lookups, ('john', 'john', 'john.com', 'john.com')):
|
||||
query = {'email__{}'.format(lookup): string}
|
||||
self.assertSequenceEqual(CITestModel.objects.filter(**query), [self.john])
|
||||
|
|
Loading…
Reference in New Issue