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

时间:2015-10-20 14:38:01

标签: django inheritance django-models django-inheritance

在我的模特中,我有练习,其中有一个m2m链接到锻炼。我还有WorkoutPlan和LogBook,它们是锻炼的类型。 WorkoutPlan是存储理想锻炼的地方。 LogBook是用户存储他们实际完成的锻炼的地方。他们还可以将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的锻炼?

1 个答案:

答案 0 :(得分: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)

然后,您可以从Exercise实例查询WorkoutPlans或LogBook:

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

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