0

在我的模型中,我有一个运动,它有一个 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 的锻炼?

4

1 回答 1

0

我认为您在这里过度使用了继承。

我猜您想将该exercises字段放入基本模型中,因为WorkoutPlan两者LogBook都有该字段。但它似乎在现实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)

然后,您可以从锻炼实例中查询锻炼计划或日志:

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

for exercise in exercise_list:
    print exercise
    workout_list = exercise.workoutplan_set.all()
    print ""
于 2015-10-20T15:28:13.360 回答