Python 使用db.allocate\u id\u range?

Python 使用db.allocate\u id\u range?,python,google-app-engine,google-cloud-datastore,webapp2,app-engine-ndb,Python,Google App Engine,Google Cloud Datastore,Webapp2,App Engine Ndb,手册中没有关于如何使用db.allocate\u id\u range的示例。我尝试了一些代码,但失败了,特别是webapp2:s用户模型,它是ndb expando模型。我想做的是创建一个用户实体,其ID号由我选择,因此我尝试使用db.allocate\u ID\u range,但它不起作用: BadArgumentError: Expected an instance or iterable of (<class 'google.appengine. ext.db.Model'>

手册中没有关于如何使用db.allocate\u id\u range的示例。我尝试了一些代码,但失败了,特别是webapp2:s用户模型,它是ndb expando模型。我想做的是创建一个用户实体,其ID号由我选择,因此我尝试使用db.allocate\u ID\u 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).
BadArgumentError:应为(,)的实例或iterable;接收用户(元模型)。
我试着这样做

first_batch=db.allocate_id_range(用户,3001301)#尝试分配id 3001


我做错了吗?我也试着把模型名加上引号,但也没用。我应该怎么做?谢谢您的建议。

您应该能够使用
ndb。分配ID
功能以实现相同的功能

如果比较和实现,您将看到它们都是底层数据存储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()
ctx=tasklets.get_context()
model.Key('Foo',1)#此处的id(1)为ingnored
开始id,结束id=ctx。分配id(键,最大值=3001)#分配所有id至3001

如果start\u id告诉您,
User
不是一个模型,而是一个元模型,不管是什么。它是什么,它从哪里来?当把模型名放在引号中时会出现什么错误?我已经更新了NDB教程。现在,它包含allocate_ids()的描述。备忘单还显示了两个allocate_id()示例(一个显示如何替换db.allocate_id_range()。感谢您的评论。我刚刚做了
User(id=5005).put()
实体已经创建,我希望我不必担心将来的冲突,即使我手动创建了分配数字ID的实体。感谢它可以创建实体,但即使我清除了数据存储,似乎也无法分配我想要的ID。ID分配器和数据存储真正独立工作,除了w当数据存储向ID分配器请求新ID时,ID分配器告诉您的只是它永远不会返回ID
start_id, end_id = Foo.allocate_ids(max=3001)
if start_id <= 3001:
    Foo(id=3001).put()