In the low-level Java api, I used to be able to make up entity names at runtime.
In python, it seems that I have to pre-define a class name that is also m开发者_如何学Pythony entity name:
class SomeKindaData(db.Expando):
pass
sKD = SomeKindaData(key_name='1')
...
Is there a way to make entity names up at run time in App Engine for Python?
I don't know much about App Engine, but you can define classes at run time like this:
def get_my_class(name):
return type(name, (db.Expando,), {})
So type takes three arguments:
- name of class
- tuple of classes to inherit from
- dictionary of class attributes
Entities themselves don't have names. In the datastore, entities are identified by keys, which can have names or IDs. You're setting your entity's key name to "1" in your sample code. Entities are also classified by kind, in this case SomeKindaData.
db.Model and db.Expando provide a local ORM abstraction around the datastore. When you use these, your entity's kind name is set to your model class name by default. If you don't want to define a model class before creating an entity, you can use the low-level datastore API:
from google.appengine.api import datastore
sKD = datastore.Entity(kind='SomeKindaData', name='1')
sKD['SomeProperty'] = 'SomeValue'
datastore.Put(sKD)
精彩评论