Skip to main content
 首页 » 编程设计

python之为什么 GQL 查询不匹配

2025年05月04日20mayingbao

我想做的是构建一些迷你 cms,其中包含带有 uri 的页面。

我的 urls.py 中的最后一条路线指向我的 views.py 中的一个函数,它会在数据存储中检查是否有可用的页面与当前请求的 uri 相同,如果有则显示该页面。

我有一个模型:

class Page(db.Model):  
  title = db.StringProperty(required=True)  
  uri = db.TextProperty(required=True)  
  created = db.DateTimeProperty(auto_now_add=True)  
  modified = db.DateTimeProperty(auto_now=True)  
  content = db.TextProperty()  

在我看来:

def show(request):  
  page = db.GqlQuery('SELECT * FROM Page WHERE uri=:uri', uri=request.path).get()  
  if page is None:  
    return http.HttpResponseNotFound()  
  else:  
    return respond(request, 'pages_show', {'content': request.path})  

而且我已经将一个带有“/work”的实体作为 uri 添加到数据存储区。

即使 request.path 恰好是“/work”,查询也不会返回匹配项。

感谢您给我的任何建议!

是的,我是一个 Python 菜鸟,App Engine 非常适合最终学习这门语言。

请您参考如下方法:

我找到了解决方案!

问题出在模型上。

App 引擎数据存储不会为 TextProperty 编制索引。使用该类型从一开始就是错误的,所以我将其更改为 StringProperty,它确实被索引,因此哪个数据存储允许我们在 WHERE 子句中使用。

工作模型示例:

   class Page(db.Model):  
      title = db.StringProperty(required=True)  
      // string property now 
      uri = db.StringProperty(required=True)  
      created = db.DateTimeProperty(auto_now_add=True)  
      modified = db.DateTimeProperty(auto_now=True)  
      content = db.TextProperty()