2015-10-20 64 views
0

在我的模型中,我有鍛鍊,其中有一個m2m鏈接到鍛鍊。我也有WorkoutPlan和LogBook,它們是鍛鍊類型。 WorkoutPlan是存放理想鍛鍊的地方。 LogBook是用戶存儲他們實際完成的鍛鍊的地方。他們還可以將LogBook鏈接到WorkoutPlan,以表明實際表現已連接到原始理想計劃。從模型中給出django繼承和m2m鏈接父母的子查詢

class Exercise(NameDescModel): 
    muscles = models.ManyToManyField(Muscle, blank=True) 
    groups = models.ManyToManyField(Group, blank=True) 
    priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0) 
    frequency = models.IntegerField() 
    time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK) 
    last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now) 

class Workout(NameDescModel): 
    exericises = models.ManyToManyField(Exercise, through='Measurement') 

class WorkoutPlan(Workout): 

    priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0) 
    frequency = models.IntegerField() 
    time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK) 
    time_estimate = models.IntegerField() 
    last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now) 

class LogBook(Workout): 
    workout_date = models.DateField(default=datetime.now) 
    notes = models.TextField(blank=True) 

    workout_plan = models.ForeignKey(WorkoutPlan, blank=True, null=True) 

對於給定的鍛鍊,我想拉的所有WorkoutPlans的,此次演習是英寸

exercise_list = Exercise.objects.order_by('-last_p_calc_date') 

for exercise in exercise_list: 
    print exercise 
    workout_list = [] 
    for workout in exercise.workout_set.all(): 
     workout_list.append(workout) 
    print list(set(workout_list)) 
    print "" 

我意識到鍛鍊的名單包括WorkoutPlans和航海日誌,因爲運動是附加到鍛鍊,而不是專門針對WorkoutPlans或LogBooks。

我該如何抽取僅附屬於WorkoutPlans的鍛鍊?

回答

0

我想你已經在這裏過度使用了繼承。

我想你想把exercises字段放到基本模型中,因爲WorkoutPlanLogBook都有這個字段。但它似乎在現實WorkoutPlanLogBook是不同類型的東西,而不是Workout的子類型。

也許並不需要的exercises田野上LogBook模式可言,因爲它有一個外鍵WorkoutPlan這似乎是一個明智的地方記錄練習......除非你想錄制的計劃之間的差異和練習實際上執行?

我想這一點,型號:

class Exercise(NameDescModel): 
    muscles = models.ManyToManyField(Muscle, blank=True) 
    groups = models.ManyToManyField(Group, blank=True) 
    priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0) 
    frequency = models.IntegerField() 
    time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK) 
    last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now) 

class WorkoutPlan(Workout): 
    exercises = models.ManyToManyField(Exercise, through='Measurement') 
    priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0) 
    frequency = models.IntegerField() 
    time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK) 
    time_estimate = models.IntegerField() 
    last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now) 

class LogBook(Workout): 
    exercises = models.ManyToManyField(Exercise, through='Measurement') 
    workout_date = models.DateField(default=datetime.now) 
    notes = models.TextField(blank=True) 
    workout_plan = models.ForeignKey(WorkoutPlan, blank=True, null=True) 

然後,您可以查詢或者WorkoutPlans或航海日誌從練習實例:

exercise_list = Exercise.objects.order_by('-last_p_calc_date') 

for exercise in exercise_list: 
    print exercise 
    workout_list = exercise.workoutplan_set.all() 
    print "" 
+0

感謝。是的,如果實際鍛鍊與計劃不同(包括或排除某些練習),我確實希望保持練習附加到記錄本。是的,我附加鍛鍊鍛鍊,因爲我想如果兩個WorkoutPlan和日誌使用它,我應該繼承。我會修改,因爲這將允許我做我需要的。 –