python - Issues with GAE allocate_ids and get_or_insert -


i'm trying combine allocate_ids() , get_or_insert() in python gae app using datastore. used

id = mymodel.allocate_ids(1)[0] key = ndb.key('mymodel', id)  m = mymodel.get_or_insert(key.id(), **{'text' : text}) 

but raises

"typeerror: name must string; received 1l".  

according guido's answer on ndb & get_or_insert how use ? (alway raise exception), have pass string get_or_insert, key.string() none. use

m = mymodel.get_or_insert(str(key.id())) 

but creates new entity, e.g. key (mymodel, '1') instead of allocated (mymodel, 1).

what's best way solve , combine both?

--

update: edit correct mistake on get_or_insert discussed in first comment thread

do not m = mymodel.get_or_insert(str(key.id())) creating complete different key, key create using numeric id.

if want functionality of get_or_insert using numeric id need replicate code in _get_or_insert_async out str check, there explicit check name being str. or write own simple.

do following inside transaction.

   id = mymodel.allocate_ids(1)[0]    key = ndb.key('mymodel', id)    obj = key.get()    if not obj:        obj = mymodel(key=key)        obj.put() 

the code inside _get_or_insert_async far more efficient though.

having said of that, if allocating id's why using get_or_insert @ all. allocate id's won't give numeric id has been allocated unless have been randomly adding numeric id's. intent of allocate id.

this makes use of get_or_insert allocated id redundant.

in code doing get_or_insert id , no other values seems meaningless operation. if using allocate ids never entity create new one, need perform second put update properties.

what trying achieve?


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -