Creating dynamic query

Hi team:
What I’m trying to do:
i’m trying to create a dynamic query which depends on which parameters are entered by the user

What I’ve tried and what’s not working:
query = {}
if self.text_box_price.text is not None or ‘’:
query[‘price’] = q.less_than_or_equal_to(int(self.text_box_price.text))
if self.text_box_make.text is not ‘’:
query[‘make’] = q.ilike(self.text_box_make.text)
if self.text_box_model.text is not ‘’:
query[‘model’] = q.ilike(self.text_box_model.text)

resp = anvil.server.call(‘getSearchedCars’, **query)

@anvil.server.callable
def getSearchedCars(**qdict):
answer = app_tables.vehicles.search(**qdict)
return answer

Problem:
i don’t know how to pass the <anvil.tables.query.ilike object> to the server

Kindly let me know what i’m missing here. Thanks!

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.

2 Likes

Thank you for your quick response. I really appreciate it. Let me give it a try

Worked like a charm! Thanks stefano