How can I query Google App Engine entities by name to convert to new names?

坚强是说给别人听的谎言 提交于 2019-12-23 06:13:34

问题


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.



来源:https://stackoverflow.com/questions/32046465/how-can-i-query-google-app-engine-entities-by-name-to-convert-to-new-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!