2013-03-26 59 views
1

我有一個模型,看起來像這樣:選擇字段是字符串名只有

class WeekOne(models.Model): 
    # Required benchmarks for given exercises 
    squatBenchmark = 1000 
    lungeBenchmark = 250 
    stairDaysCountBenchmark = 3 

    totalGoals = 4 

    squats = models.PositiveIntegerField(default=0) 
    lunges = models.PositiveIntegerField(default=0) 
    skipStairs = models.BooleanField(default=False) 
    stairDaysCount = models.PositiveSmallIntegerField(default=0) 
    # Running count of benchmarks met. 
    completeCount = models.PositiveSmallIntegerField(default=0) 
    # Set to true if benchmarks reached. 
    weekOneComplete = models.BooleanField(default=False) 

我要訪問的字段「蹲」,即在一個變量賦值amount = user.week_one.squats,但由於方式視圖和模板工作,我沒有訪問蹲領域的參考,我只有一個字符串squats。有什麼方法可以使用這個字符串來訪問該字段嗎?

回答

4

這是getattr是:

amount = getattr(user.week_one, 'squats')