[Django] #27429: had to use QuerySet.extra to do WHERE LIKE with arbitrary amount / placement of wildcard characters

26 views
Skip to first unread message

Django

unread,
Nov 3, 2016, 8:12:35 PM11/3/16
to django-...@googlegroups.com
#27429: had to use QuerySet.extra to do WHERE LIKE with arbitrary amount /
placement of wildcard characters
-------------------------------------+-------------------------------------
Reporter: Lance | Owner: nobody
Robertson |
Type: | Status: new
Uncategorized |
Component: | Version: 1.10
Uncategorized |
Severity: Normal | Keywords: QuerySet.extra
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 3, 2016, 8:13:40 PM11/3/16
to django-...@googlegroups.com
#27429: had to use QuerySet.extra to do WHERE LIKE with arbitrary amount /
placement of wildcard characters
---------------------------------+--------------------------------------
Reporter: Lance Robertson | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: QuerySet.extra | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by Lance Robertson:

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>

Django

unread,
Nov 4, 2016, 5:03:10 AM11/4/16
to django-...@googlegroups.com
#27429: had to use QuerySet.extra to do WHERE LIKE with arbitrary amount /
placement of wildcard characters
-------------------------------------+-------------------------------------
Reporter: Lance Robertson | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: QuerySet.extra | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* 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>

Reply all
Reply to author
Forward
0 new messages