如何解决Django中“计划”和“执行”模型的构造?

时间:2019-07-01 18:50:45

标签: django django-models

我正在尝试使用Django开发一个锻炼跟踪网站。

想法是,创建一个视图以“计划”和“创建”锻炼,另一个视图以“执行”所保存的锻炼之一。

计划模式

class Workout(models.Model):
    title = models.CharField(max_length=100)
    date_created = models.DateTimeField(default=timezone.now)
    date_updated = models.DateTimeField(auto_now_add=timezone.now)
    creator = models.ForeignKey(User, on_delete=models.CASCADE)

class Excercise(models.Model):
    exercise = models.CharField(max_length=100)
    sets = models.PositiveIntegerField()
    reps = models.PositiveIntegerField()
    weight = models.DecimalField(max_digits=5,decimal_places=2)
    date_created = models.DateTimeField(default=timezone.now)
    date_updated = models.DateTimeField(auto_now_add=timezone.now)
    workout = models.ForeignKey(Workout, 
    on_delete=models.CASCADE)

数据可能如下:

"Leg Workout": (workout.title)
{
  ("Squats", 5, 5, 35.5),
  ("Leg Press",3,8,55.15),
}

计划模式就是这样,可以正常使用! 现在我卡住了!

Exe-模式应如下所示:

title:Leg Workout
    exercise: Squats
        Set 1: n of 5 Reps with 35.5kg  
        Set 2: n of 5 Reps with 35.5kg
        Set 3: n of 5 Reps with 35.5kg
        Set 4: n of 5 Reps with 35.5kg
        Set 5: n of 5 Reps with 35.5kg
    exercise: Leg Press
        Set 1: n of 8 Reps with 55.15kg  
        Set 2: n of 8 Reps with 55.15kg
        Set 3: n of 8 Reps with 55.15kg

我不知道如何处理比赛,次数和重量属性。我想我必须创建一个动态模型(例如WorkoutSession),并根据定义的集合数量动态添加属性? (例如这篇帖子Django dynamic model fields

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

(如果我对您的理解正确),可以创建另一个名为Set

的模型
class Set(models.Model):
    exercise = models.ForeignKey(Excercise, on_delete.models.CASCADE)
    set_number = models.PositiveIntegerField()
    reps = models.PositiveIntegerField()
    weight = models.DecimalField(max_digits=5,decimal_places=2)
    date_created = models.DateTimeField(default=timezone.now)
    date_updated = models.DateTimeField(auto_now_add=timezone.now)

因此您将拥有例如:

Workout model instance: Leg Workout
    Exercise model instance: Squats
        Set model instance 1:
            number: 1
            reps: 5
            weight: 35.5kg  
        Set model instance 2:
            number: 2
            reps: 7
            weight: 30kg  
        ...

再说一次,这是您要的吗?