在我的模型中,我有一个运动,它有一个 m2m 链接到锻炼。我也有 WorkoutPlan 和 LogBook,它们是 Workouts 的类型。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 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)
对于给定的练习,我想提取该练习所在的所有锻炼计划。
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 和 LogBooks,因为锻炼附加到 Workout,而不是专门附加到 WorkoutPlans 或 LogBooks。
我如何提取仅隶属于 WorkoutPlans 的锻炼?