5

I'm trying to return results that have a particular phrase or word in a textfield of a particular item in a Sqlite database using peewee.

My current attempt has been:

for key in listOfKeys:
    foundPun = models.Pun.select().where(key in str(models.Pun.keywords)).get()
    click.echo('-'*10)
    click.echo(foundPun.pun)
    click.echo('-'*10)

Which returns the error: AttributeError: 'bool' object has no attribute 'clone'

For reference, this is the Pun model:

class Pun(Model):
    pun = TextField()
    keywords = TextField(default="")
    tags = TextField(default="")

class Meta:
    database = db

Is this even the right way to go about searching for results in peewee?

Any help or pointing me in the right direction is massively appreciated!

TomHill
  • 614
  • 1
  • 10
  • 26

1 Answers1

3

EDIT: Use the query operators listed here, in particular .contains(substr)

ORIGINAL:

Use fn.substr, fn documentation is here.

A similar question and more thorough answer is here.

thaavik
  • 3,257
  • 2
  • 18
  • 25
  • Thanks for the answer @thaavik - however i'm slightly confused how this helps solve the question. `fn.substr` allows me to get a substring of the textfield however I want to *check* if the substring `key` is in the `models.Pun.keyword` TextField. Could you please elaborate how I might do that with `fn.substr` ?? – TomHill Nov 27 '17 at 21:08
  • 1
    Oh, I may have misunderstood. So you want the where clause to be similar to a SQL `like` clause? i.e. `select * from pun where keywords like '%key1%'`? – thaavik Nov 27 '17 at 21:44
  • Yep - just looked that up, that's exactly what I want to achieve. I just tried `models.Pun.select(models.Pun, fn.Like('%'+key+'%'))` however that doesn't seem to be working - do you know how to correctly implement `fn.Like`? – TomHill Nov 28 '17 at 01:37