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.
You should be able to use
ndb.allocate_idsfunction 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):