Creating dynamic query

Move most of the code to the server, something like this (untested!):

# on the client
query = {}
if self.text_box_price.text is not None or '':
    query['price'] = self.text_box_price.text
if self.text_box_make.text is not '':
    query['make'] = self.text_box_make.text
if self.text_box_model.text is not '':
    query['model'] = self.text_box_model.text

resp = anvil.server.call('getSearchedCars', query)
# on the server
def getSearchedCars(query):
    qdict = {}
    if 'price' in query:
        qdict['price'] = q.less_than_or_equal_to(int(query['price']))
    if ‘make’ in query:
        qdict['make'] = q.ilike(query['make'])
    if 'model' in query:
        qdict['model'] = q.ilike(query['model'])
    answer = app_tables.vehicles.search(**qdict)
    return answer

PS: please check here for tips on how to format the code in the post.

1 Like