how would I deal with None type in this case ? I have some blank cells in the database and I need to deal with it because I am getting NoneType error:
@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 query in x['order_description']
or query in x['order_client']
or query in x['order_note']
]
return result
@jan.kyselak
It seems that you are testing your query against each column for a given row in the database.
If this is correct and you don’t want to get caught by a None
, you could test your query against the values in a dictionary, and repeat this for each row.
For example,
rows=[dict(r) for r in app_tables.tasks.search()]
result=[r for r in rows if query in r.values()]
Does that work for you?
3 Likes
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
ah great, I couln’t figure out how to write this
I have also changed the way when creating new rows so if there is nothing I better insert ‘’
Thank you
2 Likes