NDB Model.query() 上的 Python TypeError

标签 python google-app-engine typeerror

我正在关注NDB Queries Python 中的 GAE 文档,但无法继续,并出现以下错误:

# 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> 

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

最佳答案

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

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

可能值得回顾一下文档,所有内容都在那里概述。 https://developers.google.com/appengine/docs/python/ndb/queries#filter_by_prop

关于NDB Model.query() 上的 Python TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107035/

相关文章:

python - 我正在尝试创建一个简单的程序来计算百分比变化,但我在打印答案时遇到问题

python 3 : TypeError: unhashable type: 'list' when using Counter

python - skflow 回归预测多个值

python - 如何在 django 中停止用户已存在验证?

python - 命令通过终端运行,但不能通过 python 系统调用运行

python - Appengine 队列桶如何填充?

java - Google AppEngine java.net.Socket 受限错误

Python 的 "iter"函数示例给出了 TypeError

python - 列表索引超出范围 - Python 列表只有一个元素

php - 安装了 "Memcache"PECL 扩展后无法运行 PHP 运行时