使用 db.allocate_id_range?
该手册没有示例如何使用 db.allocate_id_range。我尝试了一些代码,但失败了,特别是对于 webapp2:s 用户模型,它是一个 ndb Expando 模型。我想做的只是创建一个具有我选择的 ID 号的用户实体,所以我尝试使用 db.allocate_id_range 但它不起作用:
BadArgumentError: Expected an instance or iterable of (<class 'google.appengine.
ext.db.Model'>, <class 'google.appengine.api.datastore_types.Key'>, <type 'bases
tring'>); received User<address=StringProperty('address'), auth_ids=StringProper
ty('auth_ids', repeated=True), created=DateTimeProperty('created', auto_now_add=
True), firstname=StringProperty('firstname'), lastname=StringProperty('lastname'
), notify=BooleanProperty('notify', default=False), notify_sms=BooleanProperty('
notify_sms', default=False), password=StringProperty('password'), phone_cell=Str
ingProperty('phone_cell'), registered=BooleanProperty('registered', default=Fals
e), sponsor=KeyProperty('sponsor'), updated=DateTimeProperty('updated', auto_now
=True)> (a MetaModel).
我尝试这样做的方式是这样的
first_batch = db.allocate_id_range (User, 3001, 3001) #try allocate ID 3001
我做错了吗?我还尝试将型号名称放在引号中,但这也不起作用。我应该怎么做?感谢您的任何建议。
The manual doesn't have an example how to use db.allocate_id_range. I tried some code and it failed, esxpecially for webapp2:s User model which is an ndb expando model. What I want to do is just create a User entity with an ID number of my choice so I try to use db.allocate_id_range but it is not working:
BadArgumentError: Expected an instance or iterable of (<class 'google.appengine.
ext.db.Model'>, <class 'google.appengine.api.datastore_types.Key'>, <type 'bases
tring'>); received User<address=StringProperty('address'), auth_ids=StringProper
ty('auth_ids', repeated=True), created=DateTimeProperty('created', auto_now_add=
True), firstname=StringProperty('firstname'), lastname=StringProperty('lastname'
), notify=BooleanProperty('notify', default=False), notify_sms=BooleanProperty('
notify_sms', default=False), password=StringProperty('password'), phone_cell=Str
ingProperty('phone_cell'), registered=BooleanProperty('registered', default=Fals
e), sponsor=KeyProperty('sponsor'), updated=DateTimeProperty('updated', auto_now
=True)> (a MetaModel).
The way I try to do it is like this
first_batch = db.allocate_id_range(User, 3001, 3001) #try allocate ID 3001
Am I doing it wrong? I also tried putting the model name in quotes but that didn't work either. How should I be doing this? Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用ndb.allocate_ids函数来实现相同的功能。
如果您比较 db.allocate_id_range 和 ndb allocate_ids 实现,您会看到它们都是底层数据存储区的包装器allocate_ids RPC。
如果你想用 NDB 模仿 allocate_id_range 你应该这样做:
或者更简单(就像在文档中,guido 在评论中指出):
You should be able to use
ndb.allocate_ids
function to achieve the same functionality.If you compare db.allocate_id_range and ndb allocate_ids implementation, you will see that they are both wrapper to the underlying datastore allocate_ids RPC.
If you want to mimic allocate_id_range with NDB you should be doing something like:
Or even simpler (like in the doc, guido pointed in the comment):