在 gql 查询中通过参数替换传递字符串时无法获取结果

发布于 2024-08-09 21:05:01 字数 424 浏览 9 评论 0原文

我能够通过参数替换正确地将字符串变量传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-08-16 21:05:01

您是否尝试过 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.

青瓷清茶倾城歌 2024-08-16 21:05:01

我想我已经明白了,我尝试使用它,

p = models.UserDetails.gql('WHERE user_name = :uname', uname = user_name).fetch(1)

并且得到了预期的结果集。我想知道为什么其他格式在字符串替换方面存在问题。

I think I've got it, I tried using this,

p = models.UserDetails.gql('WHERE user_name = :uname', uname = user_name).fetch(1)

and I got the expected resultset. I wonder why other formats have the problem in string substitution.

青巷忧颜 2024-08-16 21:05:01

尝试记录 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 expression user_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文