python - 构建一个对 ReferenceProperty 有条件的 GQL 查询(针对 Google App Engine)

标签 python google-app-engine gql

假设我有以下模型:

class Schedule(db.Model):
    tripCode = db.StringProperty(required=True)
    station = db.ReferenceProperty(Station, required=True)    
    arrivalTime = db.TimeProperty(required=True)
    departureTime = db.TimeProperty(required=True)

假设我有一个 Station 对象存储在 var foo 中。

如何组装一个 GQL 查询,该查询返回所有 Schedule 对象并引用 foo 引用的 Station 对象?

这是我最好的(尽管不正确)尝试形成这样的查询:

myQuery = "SELECT * FROM Schedule where station = " + str(foo.key())

再次 foo 是一个 Station 对象

最佳答案

您不应使用字符串替换将用户数据插入到 GQL 字符串中。 GQL 支持参数替换,因此您可以这样做:

db.GqlQuery("SELECT * FROM Schedule WHERE station = $1", foo.key())

或者,使用查询接口(interface):

Schedule.all().filter("station =", foo.key())

关于python - 构建一个对 ReferenceProperty 有条件的 GQL 查询(针对 Google App Engine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/852055/

相关文章:

python - 使用 Popen.communicate 时如何将警告与 stderr 中发现的错误分开?

java - 带有 GAE 和 ViewScoped ManagedBean 的 JSF2

python - Google App Engine 递归 ndb.StructuredProperty

python - 如何在 (Python) Google App Engine 数据源上正确执行一对多联接?

python - 查找 ANOVA 第一个线性模型的 F 统计值

python - SQLAlchemy 连接 - 返回一个表的所有列

google-app-engine - 限制我的 Google App Engine 应用程序访问 Google API key

google-app-engine - App Engine Datastore Viewer,如何使用 GQL 显示记录数?

google-app-engine - 在 Appengine 数据存储上查询 N 条随机记录

Python:简单类型错误