我在 Google App Engine 中使用 Cloud Datastore 时遇到一些问题。
我的数据库中有 2 个模型,它们是:
class Product(ndb.Model):
name = StringProperty(required=True)
category = KeyProperty(kind=Category)
description = TextProperty(required=True)
price = IntegerProperty(required=True)
还有
class Category(ndb.Model):
name=StringProperty(required=True)
那么我怎样才能从属于某个类别的产品类中获取所有数据呢?
在关系 SQL 中我使用:
SELECT * FROM Product WHERE category = 'CertainName';
谢谢!
最佳答案
以下应该执行您的 sql 查询正在执行的操作:
query = Product.query(Product.category == "CertainName") # Note the "=="
编辑:
我刚刚意识到你需要按键过滤(谢谢@mgilson)。查询略有变化。基本上你只需要首先获取你的category_entity,然后应用过滤器,如下所示:
query = Product.query(Product.category == category_entity.key)
例如,您可以在其中获取类别实体,如下所示:
category_entity = Category.query(Category.name == "SomeCat").fetch()
引用https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/standard/ndb/queries/snippets.py欲了解更多信息(查看query_purchases_for_customer_via_key)
关于python - 如何从 Google Cloud Datastore 检索实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39243414/