Search code examples
pythonormwhere-clausepeewee

How to add a new ".where()" into existing peewee query?


I'm trying to add a new .where to an existing peewee query and I can't. Using a debugger I see that SQL is not changed after I create the query.

My code:

query = Model.select() \
  .where(Model.year << args.years)
if args.models:
  query.where(Model.title << args.models)
if args.company:
  query.where(Model.company << args.company)
 else:
  query.where(Model.company.is_null(True))
if args.make:
  query.where(Model.make << args.make)


Solution

  • Peewee doesn't mutate in place, so you simply need to capture the return value of subsequent calls:

    if args.models:
      query = query.where(Model.title << args.models)  # Note the query =