How to use model variables in a view in Django

0

I need to calculate the age of a school report card where the data is filled in Django Admin, I would like to get each value to media and play in a table in the View.

Code:

MODEL

class Cadastro_Boletim(models.Model):

    ra_aluno = models.ForeignKey(Aluno, models.DO_NOTHING, related_name='Nome')
    curso = models.ForeignKey(Curso, models.DO_NOTHING, db_column='Curso')   # Field name made lowercase.
    nome_disciplina = models.ForeignKey(Disciplina, models.DO_NOTHING, db_column='Nome_Discplina')
    MB1 = models.SmallIntegerField(blank=True, null=True)  # Field name made lowercase.
    SUB1 = models.SmallIntegerField(blank=True, null=True)
    MB2 = models.SmallIntegerField(blank=True, null=True)
    SUB2 = models.SmallIntegerField(blank=True, null=True)
    EX = models.SmallIntegerField(blank=True, null=True)
    regular = models.BooleanField("Ativo?",default=True)

VIEW

def boletim(request):    
    contexto = {
        'boletim': Cadastro_Boletim.objects.all()   
    }
    return render(request,"boletim.html", contexto)

That way it brings everything you have in the Model, but I can not average the grades, could you help me?

    
asked by anonymous 30.11.2017 / 14:36

1 answer

1

For the average between the two months: MB1 and MB2, you can do as follows:

from django.db import AVG, F 
Cadastro_Boletim.objects.aggregate(media_bimestres=AVG(F('MB1')*F('MB2')))

Each element will then have a "media_bimes" variable available for access.

    
30.11.2017 / 20:14