构建具有 ReferenceProperty 条件的 GQL 查询(针对 Google App Engine)
假设我有以下模型:
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对象
Say I have the following model:
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)
And let's say I have a Station object stored in the var foo
.
How do I assemble a GQL query that returns all Schedule objects with a reference to the Station object referenced by foo
?
This is my best (albeit incorrect) attempt to form such a query:
myQuery = "SELECT * FROM Schedule where station = " + str(foo.key())
Once again foo
is a Station object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应使用字符串替换将用户数据插入到 GQL 字符串中。 GQL 支持参数替换,因此您可以执行以下操作:
或者使用 Query 接口:
You shouldn't be inserting user data into a GQL string using string substitution. GQL supports parameter substitution, so you can do this:
or, using the Query interface:
更简单的事情是通过将“collection_name”字段添加到 ReferenceProperty 来更改模型定义:
然后你可以这样做:
每当您想要获取所有车站的时间表时,都可以
An even easier thing to do is to change the model definition by adding the 'collection_name' field to the ReferenceProperty:
Then you can just do:
whenever you want to get all the stations' schedules.