查询重复的ndb.KeyProperty无法正常工作

时间:2014-07-03 18:34:01

标签: google-app-engine python-2.7 google-cloud-datastore app-engine-ndb

我的查询有什么问题?

以下是我的模特:

class Positions(ndb.Model):
  title = ndb.StringProperty(indexed=True)
  summary = ndb.TextProperty()
  duties = ndb.TextProperty()
  dateCreated = ndb.DateTimeProperty(auto_now_add=True)
  dateUpdated = ndb.DateTimeProperty(auto_now=True)

class Applicants(ndb.Model):
  name = ndb.StringProperty(indexed=True)
  position = ndb.KeyProperty(kind=Positions,repeated=True)
  file = ndb.BlobKeyProperty()
  dateCreated = ndb.DateTimeProperty(auto_now_add=True)
  dateUpdated = ndb.DateTimeProperty(auto_now=True)

以下是我的问题:

class AdminPositionInfoHandler(BaseHandler):
  def get(self,positionKeyId):
    user = users.get_current_user()
    if users.is_current_user_admin():
      positionKey = ndb.Key('Positions',int(positionKeyId))
      position = positionKey.get()
      applicants = Applicants.query(position=position.key).fetch() # the query
      values = {
        'position': position,
        'applicants': applicants,
      }
      self.render_html('admin-position-info.html',values)
    else:
      self.redirect(users.create_login_url(self.request.uri))

使用查询时似乎有什么问题:

applicants = Applicants.query(position=position.key).fetch()

我收到了这个错误:

File "C:\xampp\htdocs\angelstouch\main.py", line 212, in get
applicants = Applicants.query(position=position.key).fetch()
... 
TypeError: __init__() got an unexpected keyword argument 'position'

我也尝试使用positionKey代替position.key

applicants = Applicants.query(position=positionKey).fetch()

我从#34; Ancestor Queries" GAE网站部分:

https://developers.google.com/appengine/docs/python/ndb/queries

1 个答案:

答案 0 :(得分:3)

您不会将参数传递给查询 - ndb使用重写的相等/不等运算符,因此您可以更“自然地”表达查询,使用'==','<','>'等等,所以:

applicants = Applicants.query(Applications.position==position.key).fetch()

on Filtering by Property Values中的部分提供了更多示例。

(祖先是查询的特例 - 它不是模型属性)