Answer the question
In order to leave comments, you need to log in
How to properly organize the relationship between database tables?
Hello!
Below are three tables from the models.py file (the project is an online training diary):
class Exercise(models.Model):
name = models.CharField(max_length=256)
description = models.TextField(max_length=4096, null=True)
class Meta:
ordering = ['name']
def __str__(self):
return self.name
class Workout(models.Model):
RATING_CHOICES = (
('bad', 'Bad'),
('acceptable', 'Acceptable'),
('good', 'Good'),
('perfect', 'Perfect'),
)
number = models.IntegerField()
date = models.DateField()
nutrition = models.CharField(max_length=15, choices=RATING_CHOICES)
overall_health = models.CharField(max_length=15, choices=RATING_CHOICES)
comment = models.TextField(max_length=1024, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.number
class WorkoutPlan(models.Model):
workout = models.ForeignKey(Workout, on_delete=models.CASCADE) #ManyToMany, Inheritance ???
number = models.IntegerField()
exercise = models.ForeignKey(Exercise, on_delete=models.CASCADE)
weight = models.IntegerField()
reps = models.IntegerField()
sets = models.IntegerField()
Answer the question
In order to leave comments, you need to log in
As I understand it, the main table is Workout, dependent on it is WorkoutPlan, the dependency is one-to-many (there are several exercises in one task).
How are WorkoutPlan and Exercise related? one to one? via exercise id? If so, does it make sense to make 2 tables? Wouldn't it be better to combine them into one?
In my opinion, it makes sense to separate if you have a multilingual site and then Exercise will contain options for translating descriptions for different languages, and WorkoutPlan will contain all the values where translation is not needed - the weight of the barbell, etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question