Franz Noel Franz Noel - 2 months ago 8
Python Question

Receiving the requests and using If else statements on Queries with None in Python GAE

I am trying to mix and match everything, and it seems like I can't find the right answer.

I am trying to use filter on Queries. But the result is as follows: if

q!=None
, it generates a filter. But, if
q==None
, it would not display any data after using
run()


q = self.request.get('q')
if q!=None:
custQuery = db.Query(Customer)
custQuery.filter('license = ',q)
elif q==None:
custQuery = Customer.all()


But, if I replace all of them with just the following code below, it displays all the data.

custQuery = db.Query(Customer)


How do I display data if there is no q/query in an if-else statement?

Answer

I got it!

When receiving request, here are the results below. If the query is '', it will display "with blank". If the query!=None, it displays "with query". But, if the query is None, this never displays because the fact that you are asking for a q/query will always mean that the q/query is blank.

if q is None:
  a = "without query"
  #custQuery = Customer.all()
elif q is '':
  a = "with blank"
elif q!=None:
  a = "with query"

Therefore, to solve the problem, once you receive the data, you declare that the query is None. Like this:

q = self.request.get('q')
if q is '':
  custQuery = db.Query(Customer)
elif q is None:
  q = '' #Force it! (Previously: q is '' - and it works.)
  custQuery = db.Query(Customer)
elif q!=None:
  custQuery = db.Query(Customer)
  custQuery.filter('license = ',q)

Solution: Force it to be 'blank'! All data will only display if the query is 'blank'.