从给定 django 继承和 m2m link 的模型的子项查询到父项

Querying from child of model given django inheritance and m2m link to parent

在我的模型中,我有锻炼,其中有一个 m2m link 可以锻炼。我还有 WorkoutPlan 和 LogBook,它们是锻炼的类型。 WorkoutPlan 是存储理想锻炼的地方。 LogBook 是用户存储他们实际完成的锻炼的地方。他们还可以 link 一个 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 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 和 LogBooks,因为锻炼附加到 Workout,而不是专门附加到 WorkoutPlans 或 LogBooks。

我如何提取仅附属于 WorkoutPlans 的锻炼?

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

我猜您想将 exercises 字段放入基本模型中,因为 WorkoutPlanLogBook 都有该字段。但实际上 WorkoutPlanLogBook 似乎是不同类型的事物,而不是 Workout.

的子类型

可能你根本不需要 LogBook 模型上的 exercises 字段,因为它有一个指向 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 或 LogBooks:

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

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