2012-06-28 3 views
2

정수와 문자열을 서로 바꿔서 많이 db.IntegerProperty를 확장하고 싶습니다. 다음은 일부 코드 스 니펫과 App Launcher의 로그에 나타나는 오류 메시지입니다. 포인터가 있습니까? 감사 데이비드Google App Engine - db.IntegerProperty를 확장하는 방법

class FSIdProperty(db.IntegerProperty): 
    def getasstring(self): 
     value = super(FSIdProperty, self) 
     if value: 
      return "%01d" % value 
     else: 
      return '' 
    def setasstring(self, value): 
     if isinstance(value, str): 
      value = value.replace(',', '') 
      value = value.replace(' ', '') 
     newvalue = super(FSIdProperty, self) 
     newvalue = int(value) 
     return newvalue 
    asstring = property(getasstring, setasstring) 
... 
class dcccategory(db.Model): 
    categoryid = FSIdProperty(verbose_name="Category Id") 
    sortorder = FSIdProperty(verbose_name="Sort Order") 
    description = db.StringProperty(verbose_name="Description") 
    created_at = UtcDateTimeProperty(verbose_name="Created on", auto_now_add=True) 
    modifiedon = UtcDateTimeProperty(verbose_name="Modified on", auto_now=True) 
    modifiedby = db.UserProperty(verbose_name="Modified by", auto_current_user=True) 
... 
outopt = { 
     'formtitle': 'Category Maintenance', 
     'categoryid': pcategory.categoryid.asstring(), 
     'sortorder': pcategory.sortorder.asstring(), 
     'description': pcategory.description, 
     'categorys': pcategorys, 
     'formerror': ''} 
... 
    File "C:\_PythonApps\costcontrol\fcccategorymaint.py", line 17, in displayone 
    'categoryid': pcategory.categoryid.asstring(), 
AttributeError: 'int' object has no attribute 'asstring' 

답변

3

나는 NDB로 전환 권하고 싶습니다. NDB에서는 IntegerProperty의 하위 클래스를 작성하여 값을 정수로 저장하지만 정수 또는 문자열을 사용할 수 있습니다 (문자열을 정수로 변환). 다음은 스케치입니다.

class MyIntegerProperty(ndb.IntegerProperty): 
    def _validate(self, val): 
    if isinstance(val, basestring): 
     return int(val) 

그게 전부입니다!

+0

고마워, 나는 볼 것이다. –