mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #23423 -- Added unaccent lookup in django.contrib.postgres
This commit is contained in:
committed by
Tim Graham
parent
47789410db
commit
17fe0bd808
@@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.contrib.postgres.operations import HStoreExtension
|
||||
from django.contrib.postgres.operations import HStoreExtension, UnaccentExtension
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
@@ -12,4 +12,5 @@ class Migration(migrations.Migration):
|
||||
|
||||
operations = [
|
||||
HStoreExtension(),
|
||||
UnaccentExtension(),
|
||||
]
|
||||
|
||||
@@ -73,4 +73,22 @@ class Migration(migrations.Migration):
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='CharFieldModel',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('field', models.CharField(max_length=16)),
|
||||
],
|
||||
options=None,
|
||||
bases=None,
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='TextFieldModel',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('field', models.TextField()),
|
||||
],
|
||||
options=None,
|
||||
bases=None,
|
||||
),
|
||||
]
|
||||
|
||||
@@ -24,3 +24,11 @@ class NestedIntegerArrayModel(models.Model):
|
||||
|
||||
class HStoreModel(models.Model):
|
||||
field = HStoreField(blank=True, null=True)
|
||||
|
||||
|
||||
class CharFieldModel(models.Model):
|
||||
field = models.CharField(max_length=16)
|
||||
|
||||
|
||||
class TextFieldModel(models.Model):
|
||||
field = models.TextField()
|
||||
|
||||
65
tests/postgres_tests/test_unaccent.py
Normal file
65
tests/postgres_tests/test_unaccent.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import unittest
|
||||
|
||||
from django.db import connection
|
||||
from django.test import TestCase, modify_settings
|
||||
|
||||
from .models import CharFieldModel, TextFieldModel
|
||||
|
||||
|
||||
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL required')
|
||||
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
|
||||
class UnaccentTest(TestCase):
|
||||
|
||||
Model = CharFieldModel
|
||||
|
||||
def setUp(self):
|
||||
self.Model.objects.bulk_create([
|
||||
self.Model(field="àéÖ"),
|
||||
self.Model(field="aeO"),
|
||||
self.Model(field="aeo"),
|
||||
])
|
||||
|
||||
def test_unaccent(self):
|
||||
self.assertQuerysetEqual(
|
||||
self.Model.objects.filter(field__unaccent="aeO"),
|
||||
["àéÖ", "aeO"],
|
||||
transform=lambda instance: instance.field,
|
||||
ordered=False
|
||||
)
|
||||
|
||||
def test_unaccent_chained(self):
|
||||
"""
|
||||
Check that unaccent can be used chained with a lookup (which should be
|
||||
the case since unaccent implements the Transform API)
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
self.Model.objects.filter(field__unaccent__iexact="aeO"),
|
||||
["àéÖ", "aeO", "aeo"],
|
||||
transform=lambda instance: instance.field,
|
||||
ordered=False
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
self.Model.objects.filter(field__unaccent__endswith="éÖ"),
|
||||
["àéÖ", "aeO"],
|
||||
transform=lambda instance: instance.field,
|
||||
ordered=False
|
||||
)
|
||||
|
||||
def test_unaccent_accentuated_needle(self):
|
||||
self.assertQuerysetEqual(
|
||||
self.Model.objects.filter(field__unaccent="aéÖ"),
|
||||
["àéÖ", "aeO"],
|
||||
transform=lambda instance: instance.field,
|
||||
ordered=False
|
||||
)
|
||||
|
||||
|
||||
class UnaccentTextFieldTest(UnaccentTest):
|
||||
"""
|
||||
TextField should have the exact same behavior as CharField
|
||||
regarding unaccent lookups.
|
||||
"""
|
||||
Model = TextFieldModel
|
||||
Reference in New Issue
Block a user