Best way to use django for sql with raw LIKE statement

7 views
Skip to first unread message

valhalla

unread,
Nov 24, 2009, 10:50:40 PM11/24/09
to Django users
Hi,

I'm basically looking for the best way to implement something that
produces SQL similar to:

select col1, col2 from table1 where col1 like '%this%is%some%search
%string?with?wildcards'

I have worked around this with using the extra() method of the query
but this can get real messy real fast.
I have also split the string on the wild cards and chained startswith,
endswith and contains. but the contains elements don't maintain the
order that I would like (and sometime absolutely must have)

does anyone have a neat solution to this that I have missed?

Tim Valenta

unread,
Nov 24, 2009, 11:45:55 PM11/24/09
to Django users
It sounds like you've been using the right sorts of methods for that
job, but the complexity of the query is getting to be too much :)

There's a 'regex' lookup... http://docs.djangoproject.com/en/dev/ref/models/querysets/#regex

That's a pretty intense example text parsing search. I think the
usual Django philosophy might conclude that whatever you've got in
that field could be broken up into multiple fields? :) Unless it's a
TextField, but then you might be best off with regex anyway.

I can't quite wrap my brain around the requirement for such a lookup
yet, because I've never yet come across the need for any raw SQL to go
into my queries. Apologies if this doesn't really address the type of
solution you need.

Tim

valhalla

unread,
Nov 25, 2009, 1:18:51 AM11/25/09
to Django users
I knew I forgot to mention somthing....I am (not by choice) on a mssql
DB and regex is not really an option.
Thanks for the reply though, I had been using regex for this before
our db moved to MS

On Nov 25, 3:45 pm, Tim Valenta <tonightslasts...@gmail.com> wrote:
> It sounds like you've been using the right sorts of methods for that
> job, but the complexity of the query is getting to be too much :)
>
> There's a 'regex' lookup...http://docs.djangoproject.com/en/dev/ref/models/querysets/#regex

valhalla

unread,
Nov 25, 2009, 1:24:34 AM11/25/09
to Django users
Oh and as for breaking into fields its not really an option in the
cases I need unfortunately. he actual lookup would not be as log/
complex as the example but basically for the best functionality users
need to be able to do wildcard searches.



On Nov 25, 3:45 pm, Tim Valenta <tonightslasts...@gmail.com> wrote:
> It sounds like you've been using the right sorts of methods for that
> job, but the complexity of the query is getting to be too much :)
>
> There's a 'regex' lookup...http://docs.djangoproject.com/en/dev/ref/models/querysets/#regex
Reply all
Reply to author
Forward
0 new messages