You could do something like:
query = (db.mytable.myfield.like('mytext,%') | # First item in list
db.mytable.myfield.like('%,mytext,%') | # Neither first nor last
item
db.mytable.myfield.like('%,mytext') | # Last item in list
db.mytable.myfield.like('mytext')) # Single item (no list)
If you make sure to start and end every list (even single item lists) with
a separater (e.g., ",a,b,c," instead of "a,b,c"), then you can simplify it
to just:
db.mytable.myfield.like('%,mytext,%')
The above is how the DAL handles list:-type fields.
Another alternative is a regexp search:
db.mytable.myfield.regexp(r'\bmytext\b')
The above will search for "mytext" between any word boundaries -- if the
list items themselves might include word boundaries (e.g., spaces), then
you'll need a more precise regular expression.
Be careful about allowing arbitrary user input for a regexp query (see
caution
here: https://www.postgresql.org/docs/current/static/functions-matching.html).
Anthony
On Friday, December 29, 2017 at 3:24:10 AM UTC-5, Dave S wrote:
>
> I figured out how to iterate through request.vars to do a query with
> multiple .contains(). Now I want to refine that further. Is there a
> convenient way, when the field being queried is a "string list", to have
> the contains match only on word boundaries, or do I have accept partial
> matches and do further filtering on the results (the ROWS object).
>
> By "string list", I mean something similar to how list fields are
> implemented for sqlite3 backends: the field itself is a string, and the
> list elements are bounded by a separator. I'm using ',' instead of "|",
> but you may consider that a quirk for now. If I'm searching for "bit", I
> don't want "bitter" in the results.
>
> If I have to do further filtering, does filter() work on ROWS objects?
>
> BTW, I may later on replace this field with an official list-type field.
> So of course I want deluxe solution that will continue work once I get
> around to that.
>
> /dps
>
>
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.