Trying to refactor out keywordAlt bit in collectionsQuery
def countSubject(genres):
for keyword in genres:
keywordAlt = '%'+keyword+'%'
collectionsQuery = Collection.select().join(Subject).where(Subject.name ** keywordAlt, Subject.name != 'librivox', Subject.name != 'spoken', Subject.name != 'audiobook', Collection.downloads > 50)
if collectionsQuery.count() > 5:
print keyword, collectionsQuery.count()
I am refactoring this snippit, and want to first remove the keywordAlt bit and include the text in my query. However
collectionsQuery = Collection.select().join(Subject).where(Subject.name ** '%'+keyword+'%', Subject.name != 'librivox', Subject.name != 'spoken', Subject.name != 'audiobook', Collection.downloads > 50)
However, using the second method, it matches everything.
collectionsQuery = Collection.select().join(Subject).where(Subject.name ** %keyword%, Subject.name != 'librivox', Subject.name != 'spoken', Subject.name != 'audiobook', Collection.downloads > 50)
This is interpreted as a wildcard search for "keyword"
What is the proper syntax?
Ah, I think I see the problem... It has to do with python's operator precedence.
Try this:
collectionsQuery = (Collection
.select()
.join(Subject)
.where(
Subject.name ** ('%' + keyword + '%'),
# rest of query here ...
))