A
A
Alexey M2017-02-20 11:27:06
PostgreSQL
Alexey M, 2017-02-20 11:27:06

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()

The first table (Exercise) is a list of exercises and their description.
The second table (Workout) - a description of the workout with a number (not pk), date, nutrition quality rating, general condition rating, user id.
The third table (WorkoutPlan) contains the id of the workout, the number to determine the sequence of the approaches, the id of the exercise, the weight of the barbell, the number of repetitions with this weight, the number of approaches to this weight.
Each workout from the second table (Workout) will correspond to several rows of the third table (WorkoutPlan), since several exercises with different weights are performed during the workout.
I plan to make a data entry form one for two models (Workout + WorkoutPlan). My understanding is that formset_factory should be used so that the user can add rows to a third table (WorkoutPlan) with each approach performed (if it's better to use something else - please direct).
Now in the admin panel, when filling out the third table (WorkoutPlan), it is proposed to choose from the list of workouts, but in theory I don’t create a workout in advance, but enter all the information at once and I need to fill in both tables (Workout + WorkoutPlan).
Can you please tell me how to properly organize the relationship between the third and second tables so that the user enters data into both tables by filling out one form? Is it possible to use WorkoutPlan(Workout) inheritance, and if so, is it possible to make multiple lines in WorkoutPlan correspond to one line in Workout.
UPDATE 1.
Designed the database schema. Thanks to 1011 for the link to the resource.
The main problem is how to make the user enter data into both tables (Workout + WorkoutPlan) by filling out one form? after all, at the time of filling out the form, the workout in the Workout table does not yet have an id, and in WorkoutPlan I need to indicate that the completed exercises were done at this particular workout by assigning them a workout id.
a6212f138f1f4c86a440c5b09ff40387.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
1011, 2017-02-20
@1011

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 question

Ask a Question

731 491 924 answers to any question