有沒有辦法爲Model創建另一個類?將字符串轉換爲另一個Python類
例如,我有model.py,其中的類的一個內部包含:
class Customers(ndb.Model):
lastname = ndb.StringProperty()
firstname = ndb.StringProperty()
license = ndb.StringProperty()
birthdate = ndb.DateProperty()
remarks = ndb.TextProperty()
我知道我可以使用dir(model)
模型得到的,它輸出類似:
['Customers', 'CustomerAddresses','Whatever','__builtins__', '__doc__', '__file__', '__name__', '__package__', 'ndb']
但是,有沒有什麼辦法可以讓我用dir(model)
這些字符串自動生成另一個類,如:
class RestCustomers(webapp2.RequestHandler):
#some code that gets the model class attributes and displays the json string
customers = # customer attributes from model
self.response.headers['Content-Type'] = "application/json"
self.response.write(json.dumps(customers))
我不知道這是否是可能的,但有點像JavaScript的工作,其中,你這樣說
var models = dir(models); // which is an array
var class = "Rest"+models[0];
或類似的東西...