On Fri, 14 Sep 2012 17:24:21 +0300, Tomas Neme <lacrymol...@gmail.com> wrote:

I haven't done this, so I might be wrong, but...

    queryset.extra(
        where=['unaccent("table_name"."column_name"::text) LIKE
unaccent(%s)'],
        params=['%%%s%%' % value]
    )

first, wouldn't just setting params=[value] work? furthermore, isn't
that the whole point of the params thing? maybe unaccent(%%%s%%)'
params=[value].

secondly.. wouldn't chaining .extra() methods work?


Setting just params=[value] wouldn't work for me since I want to query
for rows that contain the string value, similar to how `contains` work.

Doing where=['... unaccent(%%%s%%)'], params=['value'] would produce
"... unaccent(%'value'%)" (notice the % outside the quotes) since the extra
method will quote the params before inserting them in the string.

Chaining calls to extra wouldn't work either because the where clauses will
be ANDed together.

The way I solved it is by keeping around a copy of the queryset, calling extra on the copy multiple times and ORing the whole querysets. It looks something
like this:

    original = deepcopy(queryset)
    queryset & (original.extra(...) | original.extra(...))

The ORM is smart enough to realize that the two queries are identical, except from the where clause and then it can combines them. The resulting SQL isn't
optimal but postgresql's query optimizer fixes that.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to