Google App Engine NDB ancestor query not working

2019-06-24 07:54发布

I'm trying to execute the following query:

query = Comment.query(ancestor = userKey, ndb.OR(Comment.modifiedDate > lastSyncDate, Comment.activityDate > lastSyncDate))

The 1 equal sign (=) is how the docs said we should have it, but my app doesn't run when I have just 1 equal sign (build error). If I use two equal sign, like ancestor == userKey, then the app runs, but I get a NameError: global name 'ancestor' is not defined. What gives?

I also tried another variant of this query, but the same exact problem occurs:

query = Comment.query(ndb.AND(ancestor == userKey, ndb.OR(Comment.modifiedDate > lastSyncDate, Comment.activityDate > lastSyncDate)))

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-24 08:15

You need to put the ancestor keyword after the method positional parameters:

query = Comment.query(
    ndb.OR(Comment.modifiedDate > lastSyncDate, Comment.activityDate > lastSyncDate),
    ancestor=userKey)

Alternatively, use the filters keyword explicitly, or use the .filter() method:

query = Comment.query(
    ancestor=userKey,
    filters=ndb.OR(Comment.modifiedDate > lastSyncDate, Comment.activityDate > lastSyncDate))

or

query = Comment.query(ancestor=userKey).filter(ndb.OR(Comment.modifiedDate > lastSyncDate, Comment.activityDate > lastSyncDate))
查看更多
登录 后发表回答