假设我有以下模型:
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/