views:

35

answers:

2

If searchquery is an integer that I am getting from a form; how do I convert it to string? As far as I understand this is the reason why the following code is not working (it works if searchquery is a string:

p = Pet.all().filter('score =', self.request.get('searchquery')).fetch(10)

Thank you

+1  A: 

Is this Python? I'm assuming so. In which case, use int():

p = Pet.all().filter('score =', int(self.request.get('searchquery'))).fetch(10)
Matt Ball
Hi, Thanks. But can you explain why this works. Intuitively, I would think to use str()?
Zeynel
@Zeynel: [`int()`](http://docs.python.org/library/functions.html#int) converts things to ints. [`str()`](http://docs.python.org/library/functions.html#str) converts things to strings. What about that doesn't make sense?
Matt Ball
This is how I was reasoning: I enter an integer into the search form; but the "searchquery" in the query needs to be string. So I was trying to convert "searchquery" to string. But obviously this is wrong; because int() the way you had it works. Thanks anyway; I appreciated.
Zeynel
@Zeynel: when you type **anything** in, you're entering strings. It's not magically an int just by being a string consisting solely of `[0-9]`.
Matt Ball
What Matt Ball said, request.get returns a string but the filter logic requires an int as I assume you have made 'score' an IntegerProperty in the Pet class.
pthulin
A: 

I think you need to convert an Integer to a string as far as i understand therefore use str(int) in this case : p = Pet.all().filter('score =', self.request.get(str('searchquery'))).fetch(10)

Keshan
You've got it backwards, my friend. `str()` converts things to strings.
Matt Ball
And `str('searchquery')` is a no-op, `'searchquery'` is already a string.
Jason Hall
@Jason : yes but here in this question Zeynel assumes 'searchquery' is an integer therefore he needs to convert it to a string.
Keshan
@Keshan Then you want to do `str(self.request.get('searchquery'))`
Jason Hall
ah ok sorry for the mistake :) Thanks Jason...
Keshan
`self.request.get()` will never return anything but a string.
Wooble