나는 몇일 전에 다른 유스 케이스에 대해 같은 질문을했다. 이를 해결하기 위해 필요한 변환을 수행하기 위해 속성 유형 목록을 만들었습니다. 이 솔루션은 문서화되지 않은 db 함수와 db.Model 클래스의 내부를 사용합니다. 어쩌면 더 나은 해결책이있을 수 있습니다. 좋아
class StringConversions(object)
def IntegerProperty(self, astring):
return int(astring)
def DateTimeProperty(self, astring):
# do the conversion here
return ....
그리고 그것을 사용 :
from google.appengine.ext import db
kind = 'Person'
models_module = { 'Person' : 'models'} # module to import the model kind from
model_data_types = {} # create a dict of properties, like DateTimeProperty, IntegerProperty
__import__(models_module[kind], globals(), locals(), [kind], -1)
model_class = db.class_for_kind(kind) # not documented
for key in model_class._properties : # the internals of the model_class object
model_data_types[key] = model_class._properties[key].__class__.__name__
는 같은 문자열 변환 함수가있는 클래스를 생성 할 수 있습니다, 당신의 문자열을 변환하려면
property_name = 'age'
astring = '26'
setattr(Person, property_name, getattr(StringConversions, model_data_types[property_name])(astring))
UPDATE :
에 대한 문서는 없습니다. 그러나 더 나은 해결책이 있습니다.
__import__(models_module[kind], globals(), locals(), [kind], -1)
model_class = db.class_for_kind(kind) # not documented
으로 : 이름 (db.class_for_kind)에서 엔티티 인스턴스를 구축하는 흥미/유용한 기능입니다
module = __import__(models_module[kind], globals(), locals(), [kind], -1)
model_class = getattr(module, kind)
이 ... 내 질문 죄송합니다 (약간 다릅니다하지 않을 경우이 두 줄을 교체 당신의 대답을 이해하십시오). 내 질문에 언급했듯이 문자열에서 속성 (속성 필드)을 내부화하는 것이 흥미 롭습니다. 나는 항상 문자열을 전달하고 함수가 파이썬 기본 유형에 대한 속성 유형을 기반으로 변환을 수행하고 싶습니다. (IntegerProperty 인 경우 int (strArg)를 수행 한 다음 해당 속성으로 setattr()을 수행합니다. .. (그럼에도 불구하고) – user1055761
결과 사전은 프로퍼티 이름의 모델에 프로퍼티의 데이터 유형을 제공합니다. My usecase : 두 개의 GAE 도메인 사이에서 모델 (모든 엔터티)을 교환하고 /하거나 모델의 엔터티를 압축하는 함수입니다. 이 함수는 BlobReferenceProperty, BlobProperty 및 LinkProperty (get_serving_url 포함)와 같은 데이터 유형을 변환하는 데 사용됩니다. 유스 케이스에서 결과 문자열 (Person)의 속성에 대한 데이터 유형에 의해 변환이 제어되는 문자열을 속성으로 변환 할 수 있습니다. – voscausa