For example:
{{{#!python
Pronunciation.objects.extra(where=["code LIKE '%% - %%__1 - S T __0 D'"])
}}}
I want to be able to filter with % and _ wildcards in arbitrary amounts
and places in the search string beyond just __contains, __startswith, and
__endswith, and the only way I could figure out how to do it is either to
use a raw query or .extra - if .extra goes away I hope filter will support
this behavior!
--
Ticket URL: <https://code.djangoproject.com/ticket/27429>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> My use case is that I'm trying to find phonetic patterns that rhyme using
> a database I built from the pronunciation dictionary CMUdict
>
> For example:
> {{{#!python
> Pronunciation.objects.extra(where=["code LIKE '%% - %%__1 - S T __0 D'"])
> }}}
> I want to be able to filter with % and _ wildcards in arbitrary amounts
> and places in the search string beyond just __contains, __startswith, and
> __endswith, and the only way I could figure out how to do it is either to
> use a raw query or .extra - if .extra goes away I hope filter will
> support this behavior!
New description:
My use case is that I'm trying to find phonetic patterns that rhyme using
a database I built from the pronunciation dictionary CMUdict
For example:
{{{#!python
Pronunciation.objects.extra(where=["code LIKE '%% - %%__1 - S T __0 D'"])
}}}
I want to be able to filter with % and _ wildcards in arbitrary amounts
and places in the search string beyond just contains, startswith, and
endswith, and the only way I could figure out how to do it is either to
use a raw query or .extra - if .extra goes away I hope filter will support
this behavior!
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27429#comment:1>
* status: new => closed
* resolution: => duplicate
* version: 1.10 => master
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => New feature
Comment:
It was decided that we were not going to add `like` and `ilike` lookups in
#17473.
Now that [https://docs.djangoproject.com/en/1.10/howto/custom-lookups/
custom lookups are available] you can define your own `LIKE`
implementation as follow:
{{{#!python
from django.db.models import CharField, Lookup, TextField
class LikeLookup(Lookup):
lookup_name = 'like'
def as_sql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
return '%s LIKE %s' % (lhs, rhs), params
CharField.register_lookup(LikeLookup)
TextField.register_lookup(LikeLookup)
}}}
And use like that
{{{#!python
Pronunciation.objects.filter(code__like='%% - %%__1 - S T __0 D')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27429#comment:2>