Skip to content

Instantly share code, notes, and snippets.

@osw4l
Last active September 7, 2020 18:10
Show Gist options
  • Save osw4l/dc210edd8dfab1a080cb428548e94da4 to your computer and use it in GitHub Desktop.
Save osw4l/dc210edd8dfab1a080cb428548e94da4 to your computer and use it in GitHub Desktop.
from django.db.models import Transform
class Trigram(Transform):
bilateral = True
lookup_name = 'trigram'
def as_postgresql(self, compiler, connection):
lhs, params = compiler.compile(self.lhs)
return "TRIAGRAM(%s)" % lhs, params
class Unaccent(Transform):
bilateral = True
lookup_name = 'unaccent'
def as_postgresql(self, compiler, connection):
lhs, params = compiler.compile(self.lhs)
return "UNACCENT(%s)" % lhs, params
models.CharField.register_lookup(Unaccent)
models.TextField.register_lookup(Unaccent)
models.CharField.register_lookup(Trigram)
models.TextField.register_lookup(Trigram)
from django.contrib.postgres.operations import TrigramExtension
from django.contrib.postgres.operations import UnaccentExtension
class Migration(migrations.Migration):
operations = [
TrigramExtension(),
UnaccentExtension()
]
CREATE EXTENSION unaccent;
CREATE EXTENSION pg_trgm;
CREATE EXTENSION postgis;
CREATE TEXT SEARCH CONFIGURATION unaccent ( COPY = spanish );
ALTER TEXT SEARCH CONFIGURATION unaccent ALTER MAPPING FOR hword, hword_part, word WITH unaccent, simple;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment