gql does not work for get paramters for keys
-
13-09-2019 - |
Question
I am trying to compare the key to filter results in gql in python but the direct comparision nor typecasting to int works. There fore I am forced to make a work around as mentioned in uncommented lines below. Any clues.
row = self.request.get("selectedrow")
#mydbobject = DbModel.gql("WHERE key=:1", row).fetch(1)
#mydbobject = DbModel.gql("WHERE key=:1", int(row)).fetch(1)#invalid literal for int() with base 10
#print mydbobject,row
que = db.Query(DbModel)
results = que.fetch(100)
mydbobject = None
for item in results:
if item.key().__str__() in row:
mydbobject = item
EDIT1- one more attempt that does not retrieve the record, the key exists in datastore along with record mydbobject = DbModel.gql("WHERE key = KEY('%s')"%row).fetch(1)
Solution
Am I correct in my assumption that you're basically just want to retrieve an object with a particular key? If so, the get and get_by_id methods may be of help:
mydbobject = DbModel.get_by_id(int(self.request.get("selectedrow")))
OTHER TIPS
The error "invalid literal for int()" indicate that the paramater pass to int was not a string representing an integer. Try to print the value of "row" for debuging, I bet it is an empty string.
The correct way to retrieve an element from the key is simply by using the method "get" or "get_by_id". In your case:
row = self.request.get("selectedrow")
mydbobject = DbModel.get(row)