Fixed #26610 -- Added CITextField to contrib.postgres.
This commit is contained in:
parent
7eda99f03f
commit
094d630ae8
|
@ -1,4 +1,5 @@
|
||||||
from .array import * # NOQA
|
from .array import * # NOQA
|
||||||
|
from .citext import * # NOQA
|
||||||
from .hstore import * # NOQA
|
from .hstore import * # NOQA
|
||||||
from .jsonb import * # NOQA
|
from .jsonb import * # NOQA
|
||||||
from .ranges import * # NOQA
|
from .ranges import * # NOQA
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
from django.db.models import CharField
|
||||||
|
|
||||||
|
__all__ = ['CITextField']
|
||||||
|
|
||||||
|
|
||||||
|
class CITextField(CharField):
|
||||||
|
def db_type(self, connection):
|
||||||
|
return 'citext'
|
|
@ -23,6 +23,12 @@ class CreateExtension(Operation):
|
||||||
return "Creates extension %s" % self.name
|
return "Creates extension %s" % self.name
|
||||||
|
|
||||||
|
|
||||||
|
class CITextExtension(CreateExtension):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.name = 'citext'
|
||||||
|
|
||||||
|
|
||||||
class HStoreExtension(CreateExtension):
|
class HStoreExtension(CreateExtension):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -250,6 +250,26 @@ At present using :attr:`~django.db.models.Field.db_index` will create a
|
||||||
A more useful index is a ``GIN`` index, which you should create using a
|
A more useful index is a ``GIN`` index, which you should create using a
|
||||||
:class:`~django.db.migrations.operations.RunSQL` operation.
|
:class:`~django.db.migrations.operations.RunSQL` operation.
|
||||||
|
|
||||||
|
``CITextField``
|
||||||
|
===============
|
||||||
|
|
||||||
|
.. class:: CITextField(**options)
|
||||||
|
|
||||||
|
.. versionadded:: 1.11
|
||||||
|
|
||||||
|
This field is a subclass of :class:`~django.db.models.CharField` backed by
|
||||||
|
the citext_ type, a case-insensitive character string type. Read about `the
|
||||||
|
performance considerations`_ prior to using this field.
|
||||||
|
|
||||||
|
To use this field, setup the ``citext`` extension in PostgreSQL before the
|
||||||
|
first ``CreateModel`` migration operation using the
|
||||||
|
:class:`~django.contrib.postgres.operations.CITextExtension` operation. The
|
||||||
|
code to setup the extension is similar to the example for
|
||||||
|
:class:`~django.contrib.postgres.fields.HStoreField`.
|
||||||
|
|
||||||
|
.. _citext: https://www.postgresql.org/docs/current/static/citext.html
|
||||||
|
.. _the performance considerations: https://www.postgresql.org/docs/current/static/citext.html#AEN169274
|
||||||
|
|
||||||
``HStoreField``
|
``HStoreField``
|
||||||
===============
|
===============
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,15 @@ the ``django.contrib.postgres.operations`` module.
|
||||||
|
|
||||||
Install the ``btree_gin`` extension.
|
Install the ``btree_gin`` extension.
|
||||||
|
|
||||||
|
``CITextExtension``
|
||||||
|
===================
|
||||||
|
|
||||||
|
.. class:: CITextExtension()
|
||||||
|
|
||||||
|
.. versionadded:: 1.11
|
||||||
|
|
||||||
|
Installs the ``citext`` extension.
|
||||||
|
|
||||||
``HStoreExtension``
|
``HStoreExtension``
|
||||||
===================
|
===================
|
||||||
|
|
||||||
|
|
|
@ -160,6 +160,11 @@ Minor features
|
||||||
parameter to specify a custom class to encode data types not supported by the
|
parameter to specify a custom class to encode data types not supported by the
|
||||||
standard encoder.
|
standard encoder.
|
||||||
|
|
||||||
|
* The new :class:`~django.contrib.postgres.fields.CITextField` and
|
||||||
|
:class:`~django.contrib.postgres.operations.CITextExtension` migration
|
||||||
|
operation allow using PostgreSQL's ``citext`` extension for case-insensitive
|
||||||
|
lookups.
|
||||||
|
|
||||||
:mod:`django.contrib.redirects`
|
:mod:`django.contrib.redirects`
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,9 @@ from django.db import models
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from django.contrib.postgres.fields import (
|
from django.contrib.postgres.fields import (
|
||||||
ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField,
|
ArrayField, BigIntegerRangeField, CITextField, DateRangeField,
|
||||||
FloatRangeField, HStoreField, IntegerRangeField, JSONField,
|
DateTimeRangeField, FloatRangeField, HStoreField, IntegerRangeField,
|
||||||
|
JSONField,
|
||||||
)
|
)
|
||||||
from django.contrib.postgres.search import SearchVectorField
|
from django.contrib.postgres.search import SearchVectorField
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -29,6 +30,7 @@ except ImportError:
|
||||||
|
|
||||||
ArrayField = DummyArrayField
|
ArrayField = DummyArrayField
|
||||||
BigIntegerRangeField = models.Field
|
BigIntegerRangeField = models.Field
|
||||||
|
CITextField = models.Field
|
||||||
DateRangeField = models.Field
|
DateRangeField = models.Field
|
||||||
DateTimeRangeField = models.Field
|
DateTimeRangeField = models.Field
|
||||||
FloatRangeField = models.Field
|
FloatRangeField = models.Field
|
||||||
|
|
|
@ -5,8 +5,8 @@ from django.db import migrations
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from django.contrib.postgres.operations import (
|
from django.contrib.postgres.operations import (
|
||||||
BtreeGinExtension, CreateExtension, HStoreExtension, TrigramExtension,
|
BtreeGinExtension, CITextExtension, CreateExtension, HStoreExtension,
|
||||||
UnaccentExtension,
|
TrigramExtension, UnaccentExtension,
|
||||||
)
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from django.test import mock
|
from django.test import mock
|
||||||
|
@ -15,6 +15,7 @@ except ImportError:
|
||||||
HStoreExtension = mock.Mock()
|
HStoreExtension = mock.Mock()
|
||||||
TrigramExtension = mock.Mock()
|
TrigramExtension = mock.Mock()
|
||||||
UnaccentExtension = mock.Mock()
|
UnaccentExtension = mock.Mock()
|
||||||
|
CITextExtension = mock.Mock()
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
@ -27,4 +28,5 @@ class Migration(migrations.Migration):
|
||||||
HStoreExtension(),
|
HStoreExtension(),
|
||||||
TrigramExtension(),
|
TrigramExtension(),
|
||||||
UnaccentExtension(),
|
UnaccentExtension(),
|
||||||
|
CITextExtension(),
|
||||||
]
|
]
|
||||||
|
|
|
@ -5,9 +5,9 @@ from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
from ..fields import (
|
from ..fields import (
|
||||||
ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField,
|
ArrayField, BigIntegerRangeField, CITextField, DateRangeField,
|
||||||
FloatRangeField, HStoreField, IntegerRangeField, JSONField,
|
DateTimeRangeField, FloatRangeField, HStoreField, IntegerRangeField,
|
||||||
SearchVectorField,
|
JSONField, SearchVectorField,
|
||||||
)
|
)
|
||||||
from ..models import TagField
|
from ..models import TagField
|
||||||
|
|
||||||
|
@ -138,6 +138,16 @@ class Migration(migrations.Migration):
|
||||||
options=None,
|
options=None,
|
||||||
bases=None,
|
bases=None,
|
||||||
),
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='CITextTestModel',
|
||||||
|
fields=[
|
||||||
|
('name', CITextField(primary_key=True, max_length=255)),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'required_db_vendor': 'postgresql',
|
||||||
|
},
|
||||||
|
bases=None,
|
||||||
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Line',
|
name='Line',
|
||||||
fields=[
|
fields=[
|
||||||
|
|
|
@ -2,9 +2,9 @@ from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from .fields import (
|
from .fields import (
|
||||||
ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField,
|
ArrayField, BigIntegerRangeField, CITextField, DateRangeField,
|
||||||
FloatRangeField, HStoreField, IntegerRangeField, JSONField,
|
DateTimeRangeField, FloatRangeField, HStoreField, IntegerRangeField,
|
||||||
SearchVectorField,
|
JSONField, SearchVectorField,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,6 +101,13 @@ class Character(models.Model):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class CITextTestModel(PostgreSQLModel):
|
||||||
|
name = CITextField(primary_key=True, max_length=255)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class Line(PostgreSQLModel):
|
class Line(PostgreSQLModel):
|
||||||
scene = models.ForeignKey('Scene', models.CASCADE)
|
scene = models.ForeignKey('Scene', models.CASCADE)
|
||||||
character = models.ForeignKey('Character', models.CASCADE)
|
character = models.ForeignKey('Character', models.CASCADE)
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
"""
|
||||||
|
The citext PostgreSQL extension supports indexing of case-insensitive text
|
||||||
|
strings and thus eliminates the need for operations such as iexact and other
|
||||||
|
modifiers to enforce use of an index.
|
||||||
|
"""
|
||||||
|
from django.db import IntegrityError
|
||||||
|
|
||||||
|
from . import PostgreSQLTestCase
|
||||||
|
from .models import CITextTestModel
|
||||||
|
|
||||||
|
|
||||||
|
class CITextTestCase(PostgreSQLTestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
CITextTestModel.objects.create(name='JoHn')
|
||||||
|
|
||||||
|
def test_equal_lowercase(self):
|
||||||
|
"""
|
||||||
|
citext removes the need for iexact as the index is case-insensitive.
|
||||||
|
"""
|
||||||
|
self.assertEqual(CITextTestModel.objects.filter(name='john').count(), 1)
|
||||||
|
|
||||||
|
def test_fail_case(self):
|
||||||
|
"""
|
||||||
|
Creating an entry for a citext-field which clashes with an existing
|
||||||
|
value isn't allowed.
|
||||||
|
"""
|
||||||
|
with self.assertRaises(IntegrityError):
|
||||||
|
CITextTestModel.objects.create(name='John')
|
Loading…
Reference in New Issue