Skip to content
Snippets Groups Projects
Commit 38644c68 authored by Michel, Liam (UG - Comp Sci & Elec Eng)'s avatar Michel, Liam (UG - Comp Sci & Elec Eng)
Browse files

added many-many relation between WorkoutRoutine and Exercise for easier data...

added many-many relation between WorkoutRoutine and Exercise for easier data fetching, also added the exercise list to routine view and template
parent b7d3d5a3
No related branches found
No related tags found
1 merge request!18added many-many relation between WorkoutRoutine and Exercise for easier data...
# Generated by Django 4.1.7 on 2023-05-16 22:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0005_alter_workoutroutine_workout_id'),
]
operations = [
migrations.AddField(
model_name='workoutroutine',
name='exercises',
field=models.ManyToManyField(through='core.WorkoutExercise', to='core.exercise'),
),
]
...@@ -30,7 +30,7 @@ class WorkoutRoutine(models.Model): ...@@ -30,7 +30,7 @@ class WorkoutRoutine(models.Model):
name = models.CharField(max_length = 30) name = models.CharField(max_length = 30)
description = models.CharField(max_length=50) description = models.CharField(max_length=50)
total_completions = models.IntegerField(default=0) total_completions = models.IntegerField(default=0)
exercises = models.ManyToManyField(Exercise, through= 'WorkoutExercise')
#A join table that links a workout routine with an exercise #A join table that links a workout routine with an exercise
class WorkoutExercise(models.Model): class WorkoutExercise(models.Model):
......
...@@ -6,4 +6,7 @@ ...@@ -6,4 +6,7 @@
{% block page-content %} {% block page-content %}
<div>{{routine.name}}</div> <div>{{routine.name}}</div>
<div>Access the routine object and its properties by referencing 'routine'</div> <div>Access the routine object and its properties by referencing 'routine'</div>
{% for exercise in exercises %}
<div>{{exercise.name}}</div>
{% endfor %}
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -78,8 +78,14 @@ def routine(request, routine_id): ...@@ -78,8 +78,14 @@ def routine(request, routine_id):
if user is None: if user is None:
return redirect('home') return redirect('home')
#fetch the routine using routine_id
routine = WorkoutRoutine.objects.get(workout_id = routine_id) routine = WorkoutRoutine.objects.get(workout_id = routine_id)
return render(request, 'routine.html', context={'routine': routine}) #now need to grab the exercises for that routine
#use the relation between workoutroutine and Exercise for this
exercises = routine.exercises.all()
context = {'routine':routine, 'exercises':exercises}
return render(request, 'routine.html', context)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment