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)
db.mytable.myfield.like('%,mytext,%')
db.mytable.myfield.regexp(r'\bmytext\b')
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 6:16:04 AM UTC-8, Anthony wrote: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
Thank you. I can be picky about what characters are allowed in the request, and the intended use won't be harmed by the restriction, so I may go with the regex. (And a python regex to check the user input.)
/dps
v = "xyz"
db.mytable.tags.regexp(r"(?!" + v + r")")