【问题标题】:Querying from child of model given django inheritance and m2m link to parent从模型的子节点查询给定 django 继承和 m2m 链接到父节点
【发布时间】:2015-10-20 14:38:01
【问题描述】:

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

【问题讨论】:

    标签: django inheritance django-models django-inheritance


    【解决方案1】:

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

    我猜您想将 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)
    

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

    exercise_list = Exercise.objects.order_by('-last_p_calc_date')
    
    for exercise in exercise_list:
        print exercise
        workout_list = exercise.workoutplan_set.all()
        print ""
    

    【讨论】:

    • 谢谢。是的,如果实际锻炼与计划不同(包括或不包括某些锻炼),我确实希望将锻炼附加到 LogBook。是的,我将锻炼附加到锻炼,因为我认为如果锻炼计划和日志都使用它,我应该继承。不过我会修改,因为这将使我能够做我需要做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多