0

重複の可能性:
Google App Engine - db.IntegerProperty を拡張する方法

これはおそらく、より一般的な Python クエリです。

たとえば、特別な文字列値の整数表現を返すメソッドを追加して、GAE の db.Property クラスの 1 つを継承および拡張しようとしました。

class DbHHMM(db.StringProperty):

   def to_mins(self):
      '''Convert the string to minutes''' 
      arr = re.split(":",self)
      ret = 0.0
      if (len(arr)==2):
         ret = (int(arr[0])*60)+(int(arr[1]))
      return ret;   

私のモデルでは、一連のこれらの値を合計するメソッドがあります。

class WorkSchedule(db.Model):
    '''todo - core hours for the days
    TODO is there any way to attach a widgeted form to these via newform =  WorkScheduleForm() '''

    time1 = DbHHMM() 
    time2 = DbHHMM()
    total = db.IntegerProperty

    def sum_times:
      self.total = time1.to_mins + time2.to_mins

ただし、 sum_times が呼び出されると、エラーが発生するようです:

AttributeError: 'unicode' object has no attribute 'to_mins'

GAE プロパティ クラスに追加のメソッドを追加することは可能ですか?これを防ぐために使用されている Python の手法は何ですか? 私は何か完全に間違っていますか?

4

1 に答える 1

2

実際のコードを投稿してもよろしいですか?あなたの「def sum_times:」は有効な Python でさえありません。

私の推測では、あなたはこれを望んでおり、うまくいくでしょう:

class WorkSchedule(db.Model):

    time1 = DbHHMM() 
    time2 = DbHHMM()
    total = db.IntegerProperty()  # ADDED ()

    def sum_times(self):  # ADDED (self)
        self.total = self.time1.to_mins() + self.time2.to_mins()  # ADDED self...() twice
于 2012-08-23T22:50:22.643 に答える