Refs #26610 -- Added CIText mixin and CIChar/Email/TextField.

This commit is contained in:
Mads Jensen 2017-02-11 13:16:35 +01:00 committed by Tim Graham
parent fe2d288434
commit fb5bd38e3b
7 changed files with 71 additions and 36 deletions

View File

@ -1,8 +1,20 @@
from django.db.models import CharField from django.db.models import CharField, EmailField, TextField
__all__ = ['CITextField'] __all__ = ['CICharField', 'CIEmailField', 'CIText', 'CITextField']
class CITextField(CharField): class CIText:
def db_type(self, connection): def db_type(self, connection):
return 'citext' return 'citext'
class CICharField(CIText, CharField):
pass
class CIEmailField(CIText, EmailField):
pass
class CITextField(CIText, TextField):
pass

View File

@ -250,22 +250,32 @@ 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`` ``CIText`` fields
=============== =================
.. class:: CITextField(**options) .. class:: CIText(**options)
.. versionadded:: 1.11 .. versionadded:: 1.11
This field is a subclass of :class:`~django.db.models.CharField` backed by A mixin to create case-insensitive text fields backed by the citext_ type.
the citext_ type, a case-insensitive character string type. Read about `the Read about `the performance considerations`_ prior to using it.
performance considerations`_ prior to using this field.
To use this field, setup the ``citext`` extension in PostgreSQL before the To use ``citext``, use the :class:`.CITextExtension` operation to
first ``CreateModel`` migration operation using the :ref:`setup the citext extension <create-postgresql-extensions>` in
:class:`~django.contrib.postgres.operations.CITextExtension` operation. The PostgreSQL before the first ``CreateModel`` migration operation.
code to setup the extension is similar to the example for
:class:`~django.contrib.postgres.fields.HStoreField`. Several fields that use the mixin are provided:
.. class:: CICharField(**options)
.. class:: CIEmailField(**options)
.. class:: CITextField(**options)
These fields subclass :class:`~django.db.models.CharField`,
:class:`~django.db.models.EmailField`, and
:class:`~django.db.models.TextField`, respectively.
``max_length`` won't be enforced in the database since ``citext`` behaves
similar to PostgreSQL's ``text`` type.
.. _citext: https://www.postgresql.org/docs/current/static/citext.html .. _citext: https://www.postgresql.org/docs/current/static/citext.html
.. _the performance considerations: https://www.postgresql.org/docs/current/static/citext.html#AEN169274 .. _the performance considerations: https://www.postgresql.org/docs/current/static/citext.html#AEN169274

View File

@ -210,10 +210,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 * The new :class:`~django.contrib.postgres.fields.CIText` mixin and
:class:`~django.contrib.postgres.operations.CITextExtension` migration :class:`~django.contrib.postgres.operations.CITextExtension` migration
operation allow using PostgreSQL's ``citext`` extension for case-insensitive operation allow using PostgreSQL's ``citext`` extension for case-insensitive
lookups. lookups. Three fields are provided: :class:`.CICharField`,
:class:`.CIEmailField`, and :class:`.CITextField`.
* The new :class:`~django.contrib.postgres.aggregates.JSONBAgg` allows * The new :class:`~django.contrib.postgres.aggregates.JSONBAgg` allows
aggregating values as a JSON array. aggregating values as a JSON array.

View File

@ -6,9 +6,9 @@ from django.db import models
try: try:
from django.contrib.postgres.fields import ( from django.contrib.postgres.fields import (
ArrayField, BigIntegerRangeField, CITextField, DateRangeField, ArrayField, BigIntegerRangeField, CICharField, CIEmailField,
DateTimeRangeField, FloatRangeField, HStoreField, IntegerRangeField, CITextField, DateRangeField, DateTimeRangeField, FloatRangeField,
JSONField, HStoreField, IntegerRangeField, JSONField,
) )
from django.contrib.postgres.search import SearchVectorField from django.contrib.postgres.search import SearchVectorField
except ImportError: except ImportError:
@ -30,6 +30,8 @@ except ImportError:
ArrayField = DummyArrayField ArrayField = DummyArrayField
BigIntegerRangeField = models.Field BigIntegerRangeField = models.Field
CICharField = models.Field
CIEmailField = models.Field
CITextField = models.Field CITextField = models.Field
DateRangeField = models.Field DateRangeField = models.Field
DateTimeRangeField = models.Field DateTimeRangeField = models.Field

View File

@ -2,9 +2,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, CITextField, DateRangeField, ArrayField, BigIntegerRangeField, CICharField, CIEmailField, CITextField,
DateTimeRangeField, FloatRangeField, HStoreField, IntegerRangeField, DateRangeField, DateTimeRangeField, FloatRangeField, HStoreField,
JSONField, SearchVectorField, IntegerRangeField, JSONField, SearchVectorField,
) )
from ..models import TagField from ..models import TagField
@ -136,9 +136,11 @@ class Migration(migrations.Migration):
bases=None, bases=None,
), ),
migrations.CreateModel( migrations.CreateModel(
name='CITextTestModel', name='CITestModel',
fields=[ fields=[
('name', CITextField(primary_key=True, max_length=255)), ('name', CICharField(primary_key=True, max_length=255)),
('email', CIEmailField()),
('description', CITextField()),
], ],
options={ options={
'required_db_vendor': 'postgresql', 'required_db_vendor': 'postgresql',

View File

@ -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, CITextField, DateRangeField, ArrayField, BigIntegerRangeField, CICharField, CIEmailField, CITextField,
DateTimeRangeField, FloatRangeField, HStoreField, IntegerRangeField, DateRangeField, DateTimeRangeField, FloatRangeField, HStoreField,
JSONField, SearchVectorField, IntegerRangeField, JSONField, SearchVectorField,
) )
@ -101,8 +101,10 @@ class Character(models.Model):
return self.name return self.name
class CITextTestModel(PostgreSQLModel): class CITestModel(PostgreSQLModel):
name = CITextField(primary_key=True, max_length=255) name = CICharField(primary_key=True, max_length=255)
email = CIEmailField()
description = CITextField()
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -6,25 +6,31 @@ modifiers to enforce use of an index.
from django.db import IntegrityError from django.db import IntegrityError
from . import PostgreSQLTestCase from . import PostgreSQLTestCase
from .models import CITextTestModel from .models import CITestModel
class CITextTestCase(PostgreSQLTestCase): class CITextTestCase(PostgreSQLTestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
CITextTestModel.objects.create(name='JoHn') cls.john = CITestModel.objects.create(
name='JoHn',
email='joHn@johN.com',
description='Average Joe named JoHn',
)
def test_equal_lowercase(self): def test_equal_lowercase(self):
""" """
citext removes the need for iexact as the index is case-insensitive. citext removes the need for iexact as the index is case-insensitive.
""" """
self.assertEqual(CITextTestModel.objects.filter(name='john').count(), 1) self.assertEqual(CITestModel.objects.filter(name=self.john.name.lower()).count(), 1)
self.assertEqual(CITestModel.objects.filter(email=self.john.email.lower()).count(), 1)
self.assertEqual(CITestModel.objects.filter(description=self.john.description.lower()).count(), 1)
def test_fail_case(self): def test_fail_citext_primary_key(self):
""" """
Creating an entry for a citext-field which clashes with an existing Creating an entry for a citext field used as a primary key which
value isn't allowed. clashes with an existing value isn't allowed.
""" """
with self.assertRaises(IntegrityError): with self.assertRaises(IntegrityError):
CITextTestModel.objects.create(name='John') CITestModel.objects.create(name='John')