在python中动态构建appengine模型名称

时间:2012-08-16 11:46:43

标签: python google-app-engine

我有一套模型Day1,...,Day7。 如何使用1-7的整数来使用正确的模型?

class Day1 (db.Model):    #Monday
    courtday = db.ReferenceProperty(CourtDays)
    startTime = db.TimeProperty()
    endTime = db.TimeProperty()
    startTimeLunch = db.TimeProperty()
    endTimeLunch = db.TimeProperty()
    day = "Monday"

class Day2 (db.Model):    #Tuesday
    courtday = db.ReferenceProperty(CourtDays)
    startTime = db.TimeProperty()
    endTime = db.TimeProperty()
    startTimeLunch = db.TimeProperty()
    endTimeLunch = db.TimeProperty()
    day = "Tuesday"

1 个答案:

答案 0 :(得分:1)

一天只需要一个型号,因为它们都是一样的。

class Day (db.Model):
    courtday = db.ReferenceProperty(CourtDays)
    startTime = db.TimeProperty()
    endTime = db.TimeProperty()
    startTimeLunch = db.TimeProperty()
    endTimeLunch = db.TimeProperty()
    name = db.StringProperty()

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for dayname in days:
  newday = Day(name=dayname)
  newday.put()