La liaison de paramètres à l'aide GQL dans Google App Engine
-
20-09-2019 - |
Question
Ok donc j'ai ce mode:
class Posts(db.Model):
rand1 = db.FloatProperty()
#other models here
et ce contrôleur:
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))
Alors, quand une entité est ajouté un flotteur aléatoire est généré (0-1), puis quand j'ai besoin de saisir une entité aléatoire que je veux être en mesure d'utiliser simplement une simple requête SELECT. erreurs Il avec:
BadArgumentError('Missing named arguments for bind, requires argument rand2',)
Maintenant, cela fonctionne si je vais:
posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > 1 ORDER BY rand LIMIT 1")
Il apparaît donc clairement ma requête est faux; comment peut-on utiliser une variable dans une déclaration où: S
La solution
Substitute:
"...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")
avec:
"...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1", rand2=rand2)
ou
"...WHERE rand1 > :1 ORDER BY rand LIMIT 1", rand2)
Voir pour plus d'informations: « La classe requête gql "
La chose drôle est que je viens d'apprendre cela il y a environ 2 heures: P
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow