从模型中给出django继承和m2m链接父母的子查询
问题描述:
在我的模型中,我有锻炼,其中有一个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的锻炼?
答
我想你已经在这里过度使用了继承。
我想你想把exercises
字段放到基本模型中,因为WorkoutPlan
和LogBook
都有这个字段。但它似乎在现实WorkoutPlan
和LogBook
是不同类型的东西,而不是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)
然后,您可以查询或者WorkoutPlans或航海日志从练习实例:
exercise_list = Exercise.objects.order_by('-last_p_calc_date')
for exercise in exercise_list:
print exercise
workout_list = exercise.workoutplan_set.all()
print ""
感谢。是的,如果实际锻炼与计划不同(包括或排除某些练习),我确实希望保持练习附加到记录本。是的,我附加锻炼锻炼,因为我想如果两个WorkoutPlan和日志使用它,我应该继承。我会修改,因为这将允许我做我需要的。 –