2012-07-27 75 views
1

我想將任何字符串轉換爲我現有的Entiy。是否可以寫下如下的convertToEntity()函數?谷歌應用程序引擎字符串實體

class Personel(db.Model): 
    name=db.StringProperty() 


class IsEntityExists(webapp.RequestHandler): 
    def get(self): 
    entity="Personal" 
    Entity=entity.convertToEntity() 
    Entity.all() 

回答

0

只有當你建立的模型加載...例如:

from app import model_loader 

class IsEntityExists(webapp.RequestHandler): 
    def get(self): 
    Entity=model_loader("Personal") 
    Entity.all() 

而model_loader功能將搜索的文件夾結構(Python模塊)的定義的模型。例如,你有文件夾結構:

models/ 
personal.py 
other_model.py 
user.py 

所以model_loader(「個人」)將導入personal.py,並從該模塊中提取「個人」類,允許你執行任何你想要的那類 - 如果它找到並加載它。當然,你將不得不編碼加載器。

但是,如果類(定義模式)是在同一個文件中的代碼,你可以在當地人搜索()「個人」

def load_model(name): 
    local = locals() 
    try: 
     return local[name] 
    except KeyError: 
     return None 
1

我不知道如果問題只是要求以某種方式尋找在已經導入模型類的時候給出它的名字。你可以做到這一點很容易(但只有當它已經被導入!),具體如下:

cls = db.class_for_kind("Personel") 
... cls.all() ... 

等效於NDB:

cls = ndb.Model._kind_map["Personel"] 
... cls.query() ... 

祝你好運!

PS。不,它不會進行拼寫糾正。 :-)