Querying for None and querying for all in data tables

Let’s say I have a data table with many products. Some of these products have a discontinued date and some of them do not.

So the products table looks like:

name | discontinued_date
product_1 | None
product_2 | None
product_3 | 2020-04-19
product_4 | None

I am trying to query the products data table with three different queries. Either “All”, “Not Discontinued”, or “Discontinued”. They should return:

“All”: products 1, 2, 3, and 4
“Not Discontinued”: products 1, 2, and 4
“Discontinued”: product 3

My thinking was to create a drop down in the UI with these three options. Then I could do something like:

if self.drop_down.selected_value == 'All':
   query_discontinued = # ... a query that does not filter anything
elif self.drop_down.selected_value == 'Not Discontinued':
   query_discontinued = # ... a query to get all None values
elif self.drop_down.selected_value == 'Discontinued':
   query_discontinued = # ... a query to get all not None values

My intention was then to pass this to the server side to do a search with something like

@anvil.server.callable
def get_products(query_discontinued):
  
  return app_tables.products.search(date_discontinued = query_discontinued)

It feels like there is a simple and proper way to do this. I have so far only done ways that I felt were hackish. So my question is what would be the three queries that I could write in the if statements?

Follow up question: Is there an overall better way to do this entire process?

Edit: Have a look at Searching (querying) a Table.

I think you’ll still want the ‘if’ on the server side. I’m not sure you can construct a query object on the Client and pass it over the wire…

As for passing query objects from client to server, it does indeed seem to be working.

The ‘if’ on the server side let’s me avoid the ‘All’ query. However, I still did not see a way to do the “not None” and “None” cases. Could you be more explicit?

I looked at both the Searching (querying) a Table and the anvil.tables.query API reference before initially posting.

Edit: I just noticed a link in Searching (querying) a Table for a blog post that gives more details on querying.

They have a case for filtering out None’s

Edit 2:

app_tables.machines.search(ipv6=q.not_(q.not_(None)))

seems to be working for me for the “find the Nones” case but doesn’t feel very elegant

Edit 3:

Adding even more to the chaos…

app_tables.machines.search(ipv6=q.any_of(q.not_(q.not_(None)), q.not_(None)))

seems to be working for the ‘All’ case

1 Like

Hi @gweber.lauraandleigh

If data is not sensitive, you can querry directly from client side when the permission in the table is set.

I am not too sure if it helps, just my two centa