5

假设我有以下模型:

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 对象以及对 Station 对象的引用foo

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

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

再次foo是一个Station对象

4

2 回答 2

10

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

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

或者,使用查询接口:

Schedule.all().filter("station =", foo.key())
于 2009-05-12T21:17:11.380 回答
7

更简单的做法是通过将“collection_name”字段添加到 ReferenceProperty 来更改模型定义:

station = db.ReferenceProperty(Station, required=True, collection_name="schedules")

然后你可以这样做:

foo.schedules

每当您想获得所有车站的时间表时。

于 2009-11-24T05:35:40.400 回答