How can I query Google App Engine entities by name

2019-06-14 22:39发布

问题:

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.

回答1:

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



回答2:

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.