python - 在 Google App Engine 中使用 GQL 进行参数绑定(bind)

标签 python django google-app-engine binding gql

好的,我有这个模式:

class Posts(db.Model):
  rand1 = db.FloatProperty()
  #other models here

和这个 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))

因此,当添加一个实体时,会生成一个随机 float (0-1),然后当我需要获取一个随机实体时,我希望能够只使用一个简单的 SELECT 查询。它的错误是:

BadArgumentError('Missing named arguments for bind, requires argument rand2',)

现在,如果我去,它就可以工作了:

posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > 1 ORDER BY rand LIMIT 1")

很明显我的查询是错误的;如何在 where 语句中使用变量 :S

最佳答案

替代:

 "...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")

与:

  "...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1", rand2=rand2)

或者

  "...WHERE rand1 > :1 ORDER BY rand LIMIT 1", rand2)

有关详细信息,请参阅:“The Gql query class

有趣的是我大约 2 小时前才知道这个 :P

关于python - 在 Google App Engine 中使用 GQL 进行参数绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1338704/

相关文章:

python - 如何在 django 导航栏下拉列表中显示事件链接?

java - 来自 Google App Engine 的网页中的静态图像

php - 我可以在 Google App Engine for Python 上运行 PHP 文件吗?

python - 使用不同类型的单单元阵列清理数据框列

python - 值错误 : A value in x_new is below the interpolation range

python - 有没有什么好的方法或者插件可以很方便的在vim中走到python某个函数的结尾

python - Windows 上的 GeoDjango : "Could not find the GDAL library"/ "OSError: [WinError 126] The specified module could not be found"

python - 操作数无法与形状一起广播 (128,) (0,) 错误

python - View 在模块中不存在 - 当它存在时

android - 如何在 android studio 中访问 Google Cloud Save API?