Django - How to randomly pick objects based on input from form?
I'm trying to build a basic workout planner, where you have to input in a form some info, and based on the info a personalized planner will be generated. I created a model where I inputted all the exercises, and which movement they are focusing on.
If the user choose to exercise 2x week, 2 days will be generated with 6 specific movements each day. Each movement has a number of exercises, but only one exercise will be randomly picked and showed on the planner. So you basically have 6 different exercises each day. If the user pick 3x week there will be 3 days with 6 other exercises, and so on.
I tried to find some way to randomly pick and generate those exercises, but I can't. I can't just use random, because I have the condition where each frequency picked on the form, has to follow a specific movement schedule.
So I decided to hard code it, and write each condition, line by line. But that will lead to many lines of code.
And that is not the only issue. By doing so, every time I reload the page, the exercises name change. It keeps randomly picking new exercises. Maybe I should have made the class Exercise in a different way, or I don't know. I think I' missing something and it could be much easier.
Here are my models:
class Generator(models.Model):
GOAL = (
('Strength', 'Strength'),
('Hypertophy', 'Hypertophy'),
('Endurance', 'Endurance')
)
FREQUENCY = (
('2X week', '2X week'),
('3X week', '3X week'),
('4X week', '4X week'),
('5X week', '5X week'),
('6X week', '6X week')
)
PERIOD = (
('10 weeks', '10 weeks'),
('12 weeks', '12 weeks')
)
goal = models.CharField(max_length=12, choices=GOAL)
frequency = models.CharField(max_length=12, choices=FREQUENCY)
period = models.CharField(max_length=12, choices=PERIOD, default='12 weeks')
objects = models.Manager()
def __str__(self):
return self.frequency
class Exercise(models.Model):
name = models.CharField(max_length=150)
movement = models.CharField(max_length=150)
Focus = models.CharField(max_length=150)
objects = models.Manager()
def __str__(self):
return self.name
And here are my view functions at the moment. I only did a small example of what my idea would be to code each exercise to each day (list_1 function). If I follow that idea, only the '6x week' would have 36 lines of code alone.
def create(request):
generator = GeneratorForm()
if request.method == 'POST':
generator = GeneratorForm(request.POST)
if generator.is_valid():
generator.save()
return redirect('/planner/')
context = {'generator': generator}
return render(request, 'planner/create.html', context)
def list_1(request):
generator = Generator.objects.last()
if generator.frequency == '2X week':
days = Day.objects.filter(id__in=[1, 2])
exercises_1 = Exercise.objects.filter(movement='DeadLift Pattern').order_by['?']
exercises_2 = Exercise.objects.filter(movement='Horizontal Pushing').order_by['?']
exercises = [exercises_1, exercises_2]
if generator.frequency == '3X week':
days = Day.objects.filter(id__range=(1, 3))
if generator.frequency == '4X week':
days = Day.objects.filter(id__range=(1, 4))
if generator.frequency == '5X week':
days = Day.objects.filter(id__range=(1, 5))
if generator.frequency == '6X week':
days = Day.objects.filter(id__range=(1, 6))
if generator.period == '10 weeks':
weeks = Week.objects.exclude(id__in=[11, 12])
elif generator.period == '12 weeks':
weeks = Week.objects.all()
context = {'exercises': exercises, 'days': days, 'weeks': weeks}
return render(request, 'planner/list_1.html', context)
Maybe I should make the model classes in some other way. Perhaps I need a model class for each frequency (2x wee, 3x week, etc.). I feel like there should be an easier way that I'm just missing it.
Even if the only way is to code every line like this, how to solve the issue that it picks new exercises every time I reload the page?