在 gql 查询中通过参数替换传递字符串时无法获取结果
我能够通过参数替换正确地将字符串变量传递给 gqlquery,这是我尝试使用的代码;
user_name = self.request.get('username') #retrieved from UI
p = models.UserDetails.all().filter('user_name = ', user_name).fetch(1)
我没有得到任何结果,并且查询默默失败。但是当我像这样对查询进行硬编码时,
p = models.UserDetails.all().filter('user_name = ', "peter rice").fetch(1)
我得到了预期的结果集。我认为我以错误的方式传递变量user_name
,请帮助我正确地编写我的代码。
I am able to properly pass a string variable to the gqlquery through parameter substitution, here's the code i've tried to use;
user_name = self.request.get('username') #retrieved from UI
p = models.UserDetails.all().filter('user_name = ', user_name).fetch(1)
I don't get any results and the query fails silently. But when I hard code the query like this ,
p = models.UserDetails.all().filter('user_name = ', "peter rice").fetch(1)
I get my expected resultset. I think I am passing the variable user_name
in a wrong way, Please help me in getting my piece of code right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过
filter('user_name = ', str(user_name))
?我想您确定 user_name 具有预期的内容。
Have you tried
filter('user_name = ', str(user_name))
?I supose you are sure user_name has the expected content.
我想我已经明白了,我尝试使用它,
并且得到了预期的结果集。我想知道为什么其他格式在字符串替换方面存在问题。
I think I've got it, I tried using this,
and I got the expected resultset. I wonder why other formats have the problem in string substitution.
尝试记录
repr(user_name)
以验证该字符串是否与您期望的完全相同(并且它不是 unicode 而不是原始字符串)。还可以尝试记录表达式user_name == "peter Rice"
。除此之外,我看不出它不起作用的任何原因 - API 实际上无法影响这一点,因为它不知道您传入的参数来自哪里。Try logging
repr(user_name)
to verify that the string is exactly the same as what you're expecting (and that it's not unicode rather than raw). Also try logging the expressionuser_name == "peter rice"
. Other than that, I can't see any reason why it would not work - there's literally no way the API can affect this, since it doesn't know where the argument you pass in comes from.