2012-05-08 2 views
1

연습용 추적 응용 프로그램을 만들고 모델을 만드는 가장 효율적인 방법에 대해 궁금합니다. 대부분의 운동에는 반복, 세트 및 체중이 있습니다. 그러나 거리와 시간이있는 달리기가 있습니다. 처음에는 각각을 포착 할 수있는 두 가지 모델을 만들려고했으나 그 때 결합하는 것이 더 나을 것이라고 생각했습니다. 자, 나는 잘 모르겠다. 조깅은 운동의 일종이며 다른 측정 특성을 가지고 있기 때문에 단지 밖으로 분리하기 때문에Django 모델의 올바른 구성

LEVELS = (
    (1, '1 - Hardly'), 
    (2, '2'), 
    (3, '3'), 
    (4, '4'), 
    (5, '5 - Average'), 
    (6, '6'), 
    (7, '7'), 
    (8, '8'), 
    (9, '9'), 
    (10, '10 - Very'), 

class Jog(models.Model): 
    distance = models.DecimalField("Distance (Miles)", max_digits=4, decimal_places=2) 
    time = models.DecimalField("Time (Minutes)", max_digits=4, decimal_places=2) 
    intensity = models.IntegerField("Intensity", choices = LEVELS, default = 5) 
    date = models.DateTimeField("Date", blank=True, default=datetime.now) 
    notes = models.TextField("Notes", blank=True) 

    def __str__(self): 
     return "%s Miles in %s Minutes (Intensity of %s)" % (self.distance, self.time, self.intensity) 

    class Meta: 
     verbose_name = "Jog" 

class Exercise_Type(models.Model): 
    name = models.CharField("Exercise Name", max_length=200, unique = True) 
    slug = models.SlugField(max_length=100, blank=True) 
    notes = models.TextField("Notes", blank=True) 

    def __str__(self): 
     return self.name 

class Workout(models.Model): 
    exercise_type = models.ForeignKey(Exercise_Type, verbose_name="Exercise Type") 
    reps = models.IntegerField("Repetitions") 
    sets = models.DecimalField("Sets", max_digits=2, decimal_places=1) 
    weight = models.IntegerField("Weight", blank=True, null=True) 
    intensity = models.IntegerField("Intensity", choices = LEVELS, default = 5) 
    date = models.DateTimeField("Date", blank=True, default=datetime.now) 
    notes = models.TextField("Notes", blank=True) 

이 비록 바보 같았다 :

확인은 아래에있는 내 첫 번째 패스이다. 그래서 나는 이렇게 생각했습니다. 만약 내가 이렇게하면. 운동 유형에 필요한 필드를 정의하고 사용자에게 운동의 유형을 요청하여이를 억제/사용 :

class Exercise_Type(models.Model): 
    name = models.CharField("Exercise Name", max_length=200, unique = True) 
    slug = models.SlugField(max_length=100, blank=True) 
    notes = models.TextField("Notes", blank=True) 

    distance = models.BooleanField("Use Distance Field?", default = False) 
    time = models.BooleanField("Use Time Field?", default = False) 
    reps = models.BooleanField("Use Reps Field", default = False) 
    sets = models.BooleanField("Use Sets Field?", default = False) 
    weight = models.BooleanField("Use Weight Field?", default = False) 

    def __str__(self): 
     return self.name 

class Workout(models.Model): 
    exercise_type = models.ForeignKey(Exercise_Type, verbose_name="Exercise Type") 
    distance = models.DecimalField("Distance (Miles)", max_digits=4, decimal_places=2, blank = True, null=True) 
    time = models.DecimalField("Time (Minutes)", max_digits=4, decimal_places=2, blank = True, null=True) 
    reps = models.IntegerField("Repetitions", blank = True, null=True) 
    sets = models.DecimalField("Sets", max_digits=2, decimal_places=1, blank = True, null=True) 
    weight = models.IntegerField("Weight", blank=True, null=True) 
    intensity = models.IntegerField("Intensity", choices = LEVELS, default = 5) 
    date = models.DateTimeField("Date", blank=True, default=datetime.now) 
    notes = models.TextField("Notes", blank=True) 

모든 운동에 관계없이 정보를 요구하는지의 모든 필드가 기술적으로하기 때문에 이것은 자원의 낭비처럼 보인다 아닙니다.

그런 다음 하위 분류는 어떻게됩니까? 그 때 나는 포기하고 자신보다 지식이 많은 사람들에게 호소하기로 결정했습니다.

이 모델을 구성하는 가장 좋은 방법은 무엇입니까?

답변

1

각 운동에는 측정하려는 하나 이상의 속성이 있으므로 이러한 속성을 추출해야하므로 세 가지 기본 모델이됩니다.

class Metric(models.Model): 
    name = models.CharField(max_length=20) 
    description = models.TextField(null=True, blank=True) 
    measured_in = models.CharField(max_length=20, null=True, blank=True) 
    # other fields 

class Measurement(models.Model): 
    metric = models.ForeignKey(Metric) 
    value = models.CharField(max_length=20, null=True, blank=True) 
    workout_date = models.DateField(auto_now=True) 

class Workout(models.Model): 
    # common attributes for each workout 
    name = models.CharField(max_length=200) 
    notes = models.TextField() 
    metrics = models.ManyToManyField(Measurement) 

다양한 측정 항목 (측정 항목)을 Metric에 추가합니다. 각 운동에 대해 추적하려는 통계를 확인하고 새로운 Measurement을 추가합니다. 마지막으로, 그것을 만들 때 각 운동에 연관시킵니다.

reps = Metric.objects.create(name='Reps') 
my_reps = Measurement.objects.create(metric=reps,value=10) 
w = Workout(name="Weights") 
w.metrics.add(my_reps) 
w.save() 
+1

+1, 운동 대신 운동의 일부가 아니어야합니다. 측정 값은 –

+0

입니다. 운동 (또는 운동 연대)이 여러 날짜로 나뉘며 각 날짜마다 특정 운동을 수행 할 수 있습니다 . –

+0

하지만 일반적으로 매일 운동, 별도의 하루 운동은 별도로 나열되어야합니다, 그렇지 않으면 하나의 운동 행 –

1

이 상속을 위해 만든 정확히 무엇 : 여기

는 샘플입니다. 일반 Workout 유형을 만든 다음 운동 유형으로 하위 클래스를 만듭니다. 서브 클래스의 고유/특정 속성.

class Workout(models.Model): 
    date = models.DateTimeField("Date", blank=True, default=datetime.now) 
    notes = models.TextField("Notes", blank=True) 

class Jog(Workout): 
    distance = models.DecimalField("Distance (Miles)", max_digits=4, decimal_places=2, blank = True, null=True) 
    time = models.DecimalField("Time (Minutes)", max_digits=4, decimal_places=2, blank = True, null=True) 

class Weightlifting(Workout): 
    reps = models.IntegerField("Repetitions", blank = True, null=True) 
    sets = models.DecimalField("Sets", max_digits=2, decimal_places=1, blank = True, null=True) 
    weight = models.IntegerField("Weight", blank=True, null=True) 

등등. 어디서든 일반 Workout 유형을 사용할 필요가없는 경우이를 추상 모델로 만들 수 있습니다.

+0

나는 이것에 대해 처음에는 생각했지만 데이터베이스 마이그레이션없이 다른 운동을 쉽게 추가 할 수는 없었다.실용적이지 않을 수도있는 모든 운동을 사전에 알아야합니다. –

+0

사전에 * 기본 유형의 운동을 알아야합니다. 그러나 근력 운동 분야가 극적으로 변화하지 않는 한, 호기심/심장, 체중 등 매우 많은 것들이 있습니다. 기본적인 유형의 특성은 동일하거나 적어도 "운동"보다 더 유사합니다. –