【问题标题】:Google App Engine join results of two tables两个表的 Google App Engine 连接结果
【发布时间】: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


【解决方案1】:

我实现了一个这样的字典数组:

class HomeHandler(BlogHandler):
    def get(self,user=None):
        posts = Post.query().order(-Post.created)
        all_users = User.query()
        likes = Likes.query()

        list_dict = []

        for p in posts:
            p_dict = {}
            for u in all_users:
                if p.user == u.key:
                    p_dict['p_id'] = p.key.id()
                    p_dict['p_title'] = p.title
                    p_dict['p_content'] = p.content
                    p_dict['p_created'] = p.created
                    p_dict['a_name'] = u.fullname
                    p_dict['a_id'] = u.key.id()
            for l in likes:
                if l.post == p.key:
                    p_dict['like_count'] = l.like_count
                    list_dict.append(p_dict)

        if user_email:
            self.render('home.html',user=loggedin_user,
                                    list_dict = list_dict)
        else:
            self.render('home.html',user=loggedin_user,
                                    list_dict = list_dict)

然后在模板中我像这样遍历列表并从每个字典项中获取数据:

{% if list_dict %}
      {% for l in list_dict %}
           //get stuff like this 
           // l['p_id'],etc
      {% endfor %}
{% endif %}

我发现这种方法更好,因为迭代列表和字典非常简单且耗时。

【讨论】:

  • 谁投了反对票,请帮助我提供建设性的反馈! :)
猜你喜欢
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2015-01-18
  • 1970-01-01
相关资源
最近更新 更多