【发布时间】:2016-10-04 22:21:06
【问题描述】:
我正在开发一个博客项目,使用 jinja2 作为模板,使用 Google App Engine 托管和运行它,使用 Python 作为服务器端语言。
所以,我的models.py 看起来像这样:
从 google.appengine.ext 导入 ndb
class User(ndb.Model):
fullname = ndb.StringProperty(required=True)
user_name = ndb.StringProperty(required=True)
email = ndb.StringProperty(required=True)
password = ndb.TextProperty(indexed=True,required=True)
photo = ndb.StringProperty()
location = ndb.StringProperty()
class Post(ndb.Model):
title = ndb.StringProperty(required=True)
content = ndb.TextProperty(required=True)
created = ndb.DateTimeProperty(auto_now_add=True)
last_modified = ndb.DateTimeProperty(auto_now=True)
user = ndb.KeyProperty(kind=User)
现在我想要的是有一个这样的结果集:
User.fullname | User.Photo | Post.*
我正在为单个帖子显示上述详细信息。现在每个 Post 条目都与带有用户密钥的 User 链接。
现在由于 ndb 查询类不支持连接,我如何获取和合并两个结果?
我尝试过这样的 GQL:
select User.fullname, User.photo, User.id, Post.title, Post.content, Post.created
from User, Post
where User.__key__ == Post.user
但是当我在我的数据存储控制台的 GQLQuery 中运行它时出现此错误:
GQL 查询错误:在第 2 行第 10 列遇到“,”。期待 之一:“组”,“限制”,“偏移”,“订单”,“哪里”
有什么建议吗?
TIA
EDIT:1 试过这个查询:
posts = ndb.query(User,Post).filter(User.key == Post.user).order(-Post.created)
但我收到以下错误:
TypeError: 'module' 对象不可调用
EDIT:2这是我试过的索引:
kind: Post
properties:
- name: content
- name: created
- name: user
direction: desc
但似乎不起作用:O
【问题讨论】:
-
不支持连接。您显示的查询具有隐式连接。您需要手动加入,而不是使用查询。
-
@ZigMandel:没错!这就是我尝试这个的原因:
posts = ndb.query(User,Post).filter(User.key == Post.user).order(-Post.created)但不幸的是我收到了这个错误:TypeError: 'module' object is not callable -
这又是一个联接,不支持联接。转到第一条评论。
-
但这不是显式连接。不管怎样,你对@ZigMandel 有什么建议?
-
首先要记住 - 就数据存储而言,没有表 ;-)
标签: python google-app-engine join google-cloud-datastore