从模型的小孩查询给定django继承和m2m链接到父 [英] Querying from child of model given django inheritance and m2m link to parent
问题描述
class Exercise $ D $ $ $ $ $ $ $ $ $ $ $ $ aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces aces = 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(最后优先重新计算日期,blank = True,null = True,default = datetime.now)
class Workout(NameDescModel):
exericises = models.ManyToManyField
class WorkoutPlan(锻炼):
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(最后优先重新计算日期 ,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')
练习列表中的运动:
打印练习
workout_list = []
锻炼在exercise.workout_set.all():
workout_list.append(锻炼)
打印列表(set(workout_list))
打印
我是re表明练习列表包括WorkoutPlans和LogBooks,因为锻炼附加到锻炼,而不是专门针对锻炼计划或LogBooks。
我如何将Workouts仅隶属于WorkoutPlans?
我想你在这里过度使用了继承。
我想你想把 / code>字段变为基本模型,因为
。 WorkoutPlan
和 LogBook
都具有该字段。但实际上,似乎是 WorkoutPlan
和 LogBook
是不同类型的东西,而不是<$ c的子类型$ c>锻炼
可能您不需要练习
字段 LogBook
模型,因为它有一个外键 WorkoutPlan
,这似乎是一个明智的地方来记录练习。 ..除非你想记录实际执行的计划和练习之间的差异?
我会像这样建模:
$ b $
groups = models.ManyToManyField(Group,blank = True)
$ b $ = $ $ $ $ $ $ $ $ $ $ $ True True True True True True True True True True True True True True True True True True True True True True True True True True True 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最后优先级重新计算,blank = True,null = True,default = datetime.now)
class WorkoutPlan(锻炼):
exercise = 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,选择= TIME_PERIOD_CHOICES,默认= WEEK)
time_estimate = models.IntegerField()
last_p_calc_date = models.DateField(最后优先重新计算的日期,blank = True,null = True, datetime.now)
class LogBook(锻炼):
exercise = models.ManyToManyField(Exercise,through ='Measurement')
workout_date = models.DateField(default = datetime。现在)
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:
打印练习
workout_list = exercise.workoutplan_set.all()
打印
Among my models, I have Exercise which has a m2m link to Workout. I also have WorkoutPlan and LogBook which are types of Workouts. WorkoutPlan is where ideal workouts are stored. LogBook is where a user stores the workout they actually completed. They can also link a LogBook to a WorkoutPlan to indicate that the actual performance was connected to an original ideal plan.
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)
For a given exercise, I want to pull all of the WorkoutPlans that the exercise is in.
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 ""
I'm realizing that the list of workouts include both WorkoutPlans and LogBooks because exercise is attached to Workout, not to WorkoutPlans or LogBooks specifically.
How might I pull Workouts that are affiliated only to WorkoutPlans?
I think you've over-used inheritance here.
I guess you wanted to put the exercises
field into a base model because WorkoutPlan
and LogBook
both have that field. But it seems like in reality WorkoutPlan
and LogBook
are different types of thing, rather than sub-types of Workout
.
Possibly don't you need the exercises
field on the LogBook
model at all, since it has a foreign key to WorkoutPlan
which seems a sensible place to record the exercises... unless you want to record the difference between the plan and exercises actually performed?
I would model it like this:
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)
You can then query either WorkoutPlans or LogBooks from an Exercise instance:
exercise_list = Exercise.objects.order_by('-last_p_calc_date')
for exercise in exercise_list:
print exercise
workout_list = exercise.workoutplan_set.all()
print ""
这篇关于从模型的小孩查询给定django继承和m2m链接到父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!