0

マニュアルには、db.allocate_id_range の使用例がありません。私はいくつかのコードを試してみましたが、特に ndb expando モデルである webapp2:s User モデルで失敗しました。私がやりたいことは、選択した ID 番号を持つ User エンティティを作成するだけなので、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

私はそれを間違っていますか?モデル名を引用符で囲んでみましたが、それもうまくいきませんでした。これをどのように行う必要がありますか?アドバイスをありがとう。

4

1 に答える 1

2

ndb.allocate_idsfunction を使用して同じ機能を実現できるはずです。

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()

またはさらに簡単です(ドキュメントのように、コメントで指摘されたギド):

start_id, end_id = Foo.allocate_ids(max=3001)
if start_id <= 3001:
    Foo(id=3001).put()
于 2012-02-07T18:18:16.077 に答える