NDB Model.query()上的Python TypeError

时间:2014-06-08 14:17:50

标签: python google-app-engine typeerror

我在Python中关注GAE的NDB Queries文档,但无法继续执行以下错误:

# python google_appengine/remote_api_shell.py -s 127.0.0.1:8020
App Engine remote_api shell
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2]
The db, ndb, users, urlfetch, and memcache modules are imported.
dev~env> class Customer(ndb.Model):
...     name = ndb.StringProperty()
... 
dev~env> class Purchase(ndb.Model):
...     customer = ndb.KeyProperty(kind=Customer)
...     price = ndb.IntegerProperty
... 
dev~env> c = Customer()
dev~env> c.name = 'lvqier'
dev~env> k = c.put()
dev~env> k
Key('Customer', 5629499534213120)
dev~env> p = Purchase()
dev~env> p.customer = k
dev~env> p.price = 10
dev~env> kp = p.put()
dev~env> Purchase.query(customer=c.key).fetch()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../google_appengine/google/appengine/ext/ndb/model.py", line 3209, in _query
    qry = Query(kind=cls._get_kind(), **kwds)
  File ".../google_appengine/google/appengine/ext/ndb/utils.py", line 136, in positional_wrapper
    return wrapped(*args, **kwds)
TypeError: __init__() got an unexpected keyword argument 'customer'
dev~env> 

我想要做的是获取特定客户的所有购买对象。哪里错了?

1 个答案:

答案 0 :(得分:3)

查询和过滤方法的参数需要是表达式。所以查询看起来像

Purchase.query(Purchase.customer==c.key).fetch()

可能值得回顾一下这些文档。 https://developers.google.com/appengine/docs/python/ndb/queries#filter_by_prop