google app engine - ndb unique key in range -
I am using the Google app engine and need to keep keys for an entity between 1000 and 2 ^ 31 I am considering two ways to do this:
1) Place a counter over the keys created according to the details given here. But it requires a lot of datastore which writes / writes for each key and I'm not sure that it is guaranteed to be compatible.
2) Generate a random intro in my category and check whether the key is already in the database or not. To make it cheaper, I would like to open one of the keys, but without saving my key as a separate field, there is no way to do this: MyEntity.query (MyEntity.key_field == new_random_number) .fetch (keys_only = True)
Is there a better way of achieving this?
get_or_insert example. Import 7 unique keys
import webapp2 google.appengine.ext import datetime import from dndb datetime import random import logging class example (ndb.model): data = ndb.StringProperty () modified = Ndb .DateTimeProperty (auto_now = true) Created = ndb.DateTimeProperty () #No auto_now_add here! Class manhandler (webapp2.RequestHandler): def mill (self): count = 0 while count & lt; 7: random_key = str (random.randrange (1, 9)) dt_created = datetime.now () example = example .get_or_insert (random_key, created = dt_created, data = 'for some data' + random_key) Example example.created! = Dt_created: logging.warning ('random key% s not unique'% random_key) count + = 1 self.response.write ('key inclusive') Continue app = webapp2.WSGIApplication ([('/', mainhandler]] , Debug = true)
Comments
Post a Comment