How can I query Google App Engine entities by name

2019-06-14 22:33发布

I want to convert some Entities to new names. How can I query entities not having model class defined.

For example I have such entity (it simplified to be more readable):

class Some(ndb.model):
  name = ndb.StringProperty()

I want to rename it to:

class SomeFile(ndb.model):
  name = ndb.StringProperty()

How can I do it?

If will rename Some to SomeFile there will be no more Some to query but only data in datastore.

2条回答
2楼-- · 2019-06-14 22:34

Perhaps I don't understand your question, but is this what you want?:

for x in Some.query():
    y = SomeFile()
    y.name = x.name
    y.put()
    x.key.delete()

Although you should make this more efficient by doing it in batches.

查看更多
可以哭但决不认输i
3楼-- · 2019-06-14 22:53

You can change your Model class name and have it point to an existing datastore Kind by overriding the Model's _get_kind() method.

class SomeFile(ndb.Model):
    @classmethod
    def _get_kind(cls):
      return 'Some'

Now you can use SomeFile in python code while retaining the Some entities in your datastore.

https://cloud.google.com/appengine/docs/python/ndb/modelclass#introduction

查看更多
登录 后发表回答