1👍
✅
Since each exercise has one or more properties that you want to measure, you should extract those properties out, so you end up with three main models.
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)
You add various metrics (things you measure) to Metric
. For each workout, you identify which metric you want to track, by adding creating a new Measurement
. Finally, you associate it to each workout when you create it.
Here is a sample:
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👍
This is exactly what inheritance is made for. Create a generic Workout
type, and then subclass it with the types of workouts. With the unique/specific attributes on the subclasses.
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)
And so on. If you don’t need to use the generic Workout
type anywhere, you can make it an abstract model.
- [Answered ]-In Django, how do I generate the CSRF input element while in a view?
- [Answered ]-Django and Rails with one common DB
- [Answered ]-Django Rest Framework custom user model PUT Request
- [Answered ]-How to use signals to update another object in Django?
Source:stackexchange.com