Question

I am a NDB user and this object database is quite cool. But how can I seed specific default values directly after deployment? Is there some predefined functionality or standardized way for database seeding?

As example:

I have the following ndb.Model and want some sort of "existing default parent".

Category(ndb.Model):
    name = ndb.StringProperty(required=True)
    parent = ndb.KeyProperty(kind='Category',required=True, 
                   default=<KeyOfRootCategory>)

Where to put the following seeding values?

main_category = Category(name="all", parent=None) #this is the root category
main_category.put()
Was it helpful?

Solution

Doesn't look like there are dedicated 'post-deployment' hooks for that. I'd simply put some code into the main handler script (the one that contains 'webapp2.WSGIApplication(...)') checking if the root category exists already and create it if not. Alternatively this could be part of some handler action.

OTHER TIPS

Why not create a simple seeding handler to call after deployment (e.g. /seeding/example)? The way I see it you only have to seed once so there's no need for some sort of hook.

seed.py:

class ExampleHandler(webapp2.RequestHandler):

    def get(self):

        # Do your thing
        # Maybe use "get_or_insert()". See [1]

        return

app = webapp2.WSGIApplication(
    [
        ('/example', ExampleHandler),
    ],
    debug=True

)

Then in your app.yaml:

- url: /seeding/.*
  script: seed.app
  login: admin

The last line is crucial. It protects your seeding script from unauthorized access (see [2]).

[1] https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_get_or_insert

[2] https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Requiring_login_or_administrator_status

I think I understand what you are asking.

You can create a parent key without having to create the entity. That will define your entity group.

Alternately it doesn't need a parent, but will be the parent of any child. Any entity with out a parent defined in the key becomes the root of it's own entity group, and that entity group can have 1 or more members (i.e itself and any children.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top