Comparing string to each value in row - NoneType not iterable

Calling dict() on a database row is rather slow in our current implementation (we’re working on it!).

But it’s actually simpler to use a neat feature of Python, namely the or operator. None is “falsy”, so for the expression A or B, if A is None, that expression will return B. So we use x['column'] or "" to return the value of the column (if it’s not None), or an empty string (if it is None).

Here’s what that would look like on your sample code:

@anvil.server.callable
def search_order(query):
 
  result = app_tables.tasks.search()
  if query:
    result = [
      x for x in result
      if query in (x['order_number'] or "")
      or query in (x['order_description'] or "")
      or query in (x['order_client'] or "")
      or query in (x['order_note'] or "")
    ]
  return result
5 Likes