使用 db.allocate_id_range?

发布于 2025-01-02 18:27:59 字数 1133 浏览 2 评论 0原文

该手册没有示例如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2025-01-09 18:27:59

您应该能够使用ndb.allocate_ids函数来实现相同的功能。

如果您比较 db.allocate_id_rangendb allocate_ids 实现,您会看到它们都是底层数据存储区的包装器allocate_ids RPC。

如果你想用 NDB 模仿 allocate_id_range 你应该这样做:

ctx = tasklets.get_context()
model.Key('Foo', 1) # the id(1) here is ingnored
start_id, end_id = ctx.allocate_ids(key, max=3001) # allocate all ids up to 3001
if start_id <= 3001:
    # it is safe to use 3001
    Foo(id=3001).put()

或者更简单(就像在文档中,guido 在评论中指出):

start_id, end_id = Foo.allocate_ids(max=3001)
if start_id <= 3001:
    Foo(id=3001).put()

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:

ctx = tasklets.get_context()
model.Key('Foo', 1) # the id(1) here is ingnored
start_id, end_id = ctx.allocate_ids(key, max=3001) # allocate all ids up to 3001
if start_id <= 3001:
    # it is safe to use 3001
    Foo(id=3001).put()

Or even simpler (like in the doc, guido pointed in the comment):

start_id, end_id = Foo.allocate_ids(max=3001)
if start_id <= 3001:
    Foo(id=3001).put()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文