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