python - 如何使用ListProperty(数字列表中的项目必须都是Key实例)

标签 python google-app-engine

第一个模型

    class A(db.Model):
      number = db.IntgerProperty()

第二个模型

class B(db.Model):
   name = db.StringProperty()
   numbers = db.ListProperty(db.Key)

A模型已成功保存。

a = A()
a.number = 90
a.put()

BadValueError:数字列表中的项目必须都是 Key 实例

b = B()
b.name = 'test'
b.numbers = [90]
b.put()

请帮忙吗?非常感谢。

最佳答案

编辑(基于OP评论):如果您想插入db.Key,您需要提供一个。
调用 put() 返回一个键,因此您可以使用它,例如:

a = A()
the_key = a.put()

b = B()
b.name = 'test'
b.numbers = [the_key]
b.put()
<小时/>

OP澄清之前的旧答案:

使用 db.ListProperty(db.Key),您表示需要 db.Key 实例的列表。
如果您想要整数,请像这样声明您的模型:

class B(db.Model):
   name = db.StringProperty()
   numbers = db.ListProperty(int)
                             ^^^ 

关于python - 如何使用ListProperty(数字列表中的项目必须都是Key实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827201/

相关文章:

python - 如何使用 reduce/map/list comprehension 求和?

python - GAE - 按属性获取()实体?

python - 我需要一个 Python 函数,当给定所需的字符概率时,它将输出 4 个不同字符的随机字符串

java - App Engine 聊天应用程序

php - 无法通过本地 php 实例连接到数据存储

python - 向现有 python 脚本添加浏览器功能

python - 从原始文本中提取信息

python - 从登录我的 GAE 应用程序的用户发送电子邮件

python - 如何使用变量用法而不是实际名称来访问 GAE 数据存储属性?

google-app-engine - Google AppEngine Bulkloader 高复制数据延迟