Google应用程序引擎Python-数据存储:具有db.ReferenceProperty()的条件
所以,我有两个模型:作者和帖子。两者都有一个布尔字段“status”。文章由作者完成,因此db.ReferenceProperty()字段。这些模型是:Google应用程序引擎Python-数据存储:具有db.ReferenceProperty()的条件,python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,所以,我有两个模型:作者和帖子。两者都有一个布尔字段“status”。文章由作者完成,因此db.ReferenceProperty()字段。这些模型是: class Authors(db.Model): status = db.BooleanProperty(default = True) name = db.StringProperty(required = True) class Posts(db.Model): status = db.BooleanPropert
class Authors(db.Model):
status = db.BooleanProperty(default = True)
name = db.StringProperty(required = True)
class Posts(db.Model):
status = db.BooleanProperty(default = True)
title = db.StringProperty(required = True)
content = db.TextProperty(required = True)
author = db.ReferenceProperty(Authors)
因此,当状态字段(文章和引用的作者)都设置为True时,我希望能够在我的站点上列出文章。如果我将Authors.status设置为False,它的所有子帖子将自动不再显示
我知道这是行不通的,但应该是这样的:
q = Posts.all()
q.filter('status =', True)
q.filter('author.status =', True)
q.run()
我知道这是一个连接,GAE数据存储不支持连接,但我怎么可能做到这一点呢?提前感谢。正如您所说,您不能使用数据存储进行连接。因此,您只能迭代并检查状态 具体做法取决于您的数据。您可能希望首先查询作者,然后获取每个具有正确状态的作者的帖子:
all_posts = []
q = Authors.all().filter('status', True)
for author in q:
posts = Post.all().filter('author', author).filter('status', True)
all_posts.extend(posts.run())
另一种方法是获取status=True的所有作者的密钥,将它们放在一个集合中,然后遍历所有帖子并检查是否有作者密钥:
all_posts = []
authors = set(Authors.all(keys_only=True).filter('status', True).run())
q = Post.all().filter('status', True)
for post in q:
if post._author in authors:
all_posts.append(post)
正如我所说,哪个更有效取决于你有多少不同的作者,每个人有多少帖子,以及每个人的状态分布。尝试它们并检查生成的查询数量