I have to calculate a series of data for my project, it works 100% when it comes to general calculation, but when I try to take those same calculations to the facilities ( jovenclubs
) separately it returns me a list with all the values it contains:
example: joven club
x has a field called Natural
, with a series of data, which I want to add.
This is the view of the general values , it works fine:
def inicio(request):
plan_gral = jovenclub.objects.aggregate(sum=Sum('plan_gral'))
juridic_gral = ingresos.objects.aggregate(sum=Sum('juridico'))
natural_gral = ingresos.objects.aggregate(sum=Sum('natural'))
general = juridic_gral['sum']+natural_gral['sum']
diferencia = plan_gral['sum']-general
porciento_general = general*100/plan_gral['sum']
ingreso = ingresos.objects.all()
mes = datetime.now().month
meses=ingresos.objects.filter(fecha__month = mes)
year = datetime.now().year
amo=ingresos.objects.filter(fecha__year = year)
and this is the view of the installations :
def detalle_jc(request, id_jovenclub ):
jc = jovenclub.objects.get(pk=id_jovenclub)
datos=ingresos.objects.all()
print jc.ingresos_set.all()
sum=0
for ingreso in jc.ingresos_set.all():
sum=ingreso.get_importe()
sum=(round((sum/jc.plan_gral)*100,2))
This is the call in the template :
{% for ingreso in jc.ingresos_set.all %}
{{ ingreso.natural }}
{% endfor %}
Return the following:
3974.0 5200.0 4875.0
These are the data in the field Natural
, which should add
model.py:
class jovenclub(models.Model):
nombre = models.CharField(max_length=15, choices=JOVENCLUB)
especialista = models.CharField(max_length=50)
plan_gral = models.FloatField()
def __unicode__(self):
return self.nombre
class ingresos(models.Model):
# mes = models.IntegerField(validators=[no_negativo])
jovenclub = models.ForeignKey(jovenclub)
natural = models.FloatField(blank=True)
juridico = models.FloatField(blank=True)
fecha = models.DateField()
def get_importe(self):
return self.natural+self.juridico
importe = property(get_importe)