Parameter binding using GQL in Google App Engine
So, I have this mode:
class Posts(db.Model):
rand1 = db.FloatProperty()
#other models here
and this controller:
class Random(webapp.RequestHandler):
def get(self):
rand2 = random.random()
posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")
#Assigning values for Django templating
template_values = {
'posts_query': posts_query,
#test purposes
'rand2': rand2,
}
path = os.path.join(os.path.dirname(__file__), 'templates/random.html')
self.response.out.write(template.render(path, template_values))
So, when an object is added, a random float (0-1) is generated, and then when I need to grab a random object, I want to be able to just use a simple SELECT query. These are errors with:
BadArgumentError('Missing named arguments for bind, requires argument rand2',)
Now this works if I go:
posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > 1 ORDER BY rand LIMIT 1")
Thus, my request is incorrect; how to use variable in where statement: S
+2
source to share
1 answer
Replacement:
"...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")
from:
"...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1", rand2=rand2)
or
"...WHERE rand1 > :1 ORDER BY rand LIMIT 1", rand2)
. For more information see: " Class Gql query "
The funny thing is that I just found out about this 2 hours ago: P
+3
source to share