Home > Blockchain >  Sum using Django Template syntax?
Sum using Django Template syntax?

Time:12-06

I have an exercise that consists of sets, I assign sets to this exercise model. I am able to calculate the total volume of a single set. But how do I calculate the total volume for the exercise. Is it possible to return the sum value of a for loop? When I do it in the view I am only able to return the sum of .all() like this

def homeview(request):
    set = ExerciseSetModel.objects.all()
    sum = 0
    for obj in set:
        sum  = obj.setvolume()

    context = {
        'sum': sum, # returns the sum of everything but I want the single exercise
    }

    return TemplateResponse(request, 'poststemplates/posthome.html', context)

model:

class ExerciseModel(models.Post):
    exercisename = models.TextField(max_length=500, verbose_name='text',
                                blank=True, null=True, default="text")

class ExerciseSetModel(models.Post):
    exercise = models.ForeignKey(ExerciseModel, on_delete=models.CASCADE, default=None,)
    repsnumber = models.IntegerField(default=1, null=True, blank=True)
    weightnumber = models.IntegerField(default=1, null=True, blank=True)

    def setvolume(self):
    return self.repsnumber * self.weightnumber

views:

@login_required
def homeview(request):

    exercises = ExerciseModel.objects.all()

    context = {
        'exercises': exercises,
    }
    return TemplateResponse(request, 'poststemplates/posthome.html', context)

template:

{% for exercise in exercises %}
  {% for set in exercise.exercisesetmodel_set.all %}
    {{set.setvolume}}
  {% endfor %}
{% endfor %}

now let's say that I have two sets inside a single exercise, I'm able to calculate the set volume for each set, but how would I calculate the exercise volume for the entire exercise esentially calculating setvolume 1 setvolume 2 adding up each iteration in the for loop

CodePudding user response:

How about below options? These are way to detour lack of functionality of django template. If you have trouble when process something in template, you can consider doing that in views.

Option 1: Calculating total sum(sum all of sets by exercise) in views and passing {exercise.id:total_sum} dictionary to template

views.py

def homeview(request):

    exercises = ExerciseModel.objects.all()
    total_sum_by_exercise = {}
    for exercise in exercises:
        sets = ExerciseSetModel.objects.filter(exercise=exercise).all()
        sum_all_sets = 0
        for obj in sets:
            sum_all_sets  = obj.setvolume()
        total_sum_by_exercise[exercise.id] = sum_all_sets

    context = {
        'exercises': exercises, 'total_sum_by_exercise': total_sum_by_exercise
    }
    return TemplateResponse(request, 'poststemplates/posthome.html', context)

posthome.html

{% for exercise in exercises %}

  {% for set in exercise.exercisesetmodel_set.all %}
    {{set.setvolume}}
  {% endfor %}

  {% for key, val in total_sum_by_exercise.items %}
    {% if key == exercise.id%}
      {{val}}
    {% endif %}

  {% endfor %}
{% endfor %}

Option 2: Defining method for calculating total sum in model, and call that method in template

models.py

class ExerciseModel(models.Model):
    exercisename = models.TextField(max_length=500, verbose_name='text', blank=True, null=True, default="text")

    def total_sum(self):
        sets = ExerciseSetModel.objects.filter(exercise=self.id).all()
        sum_all_sets = 0
        for obj in sets:
            sum_all_sets  = obj.setvolume()
        return sum_all_sets

class ExerciseSetModel(models.Model):
    exercise = models.ForeignKey(ExerciseModel, on_delete=models.CASCADE, default=None,)
    repsnumber = models.IntegerField(default=1, null=True, blank=True)
    weightnumber = models.IntegerField(default=1, null=True, blank=True)

    def setvolume(self):
        return self.repsnumber * self.weightnumber

posthome.html

{% for exercise in exercises %}

  {% for set in exercise.exercisesetmodel_set.all %}
    {{set.setvolume}}
  {% endfor %}
  
  {{exercise.total_sum}}

{% endfor %}
  • Related