Problems in data rendering django with annotate [closed]

0

In the view.py I have a query like this

ing =ingresos.objects.filter(pk=id_jovenclub).values('natural').annotate(sum=Sum('natural'))

in the template is like this

{{ ing}}

and it returns these values

[{'sum': 1066.0, 'natural': 1066.0}]

Because the data are duplicated and others as an achievement the characters disappear [{}]

eye that only happens with annotate because with aggregate I get the clean value

    
asked by Roly Miranda Díaz 02.05.2017 в 20:23
source

1 answer

1

Let's see, first of all the characters [{}]. The filter function always returns a list so the square brackets []. If you just wait for a result what you could do in the template is something like

{{ ing[0] }}

The inner braces mean that it is a field within the query, if you just want to get a field you could do the following.

{{ ing[0].sum }}

Where 'sum' is a query field.

The second. I would need to see the records in the database to be sure but possibly what is happening is that there is only one value in 'natural' (which is 1066.0), that is to say when making the sum of all 'natural' you only find one so that the result of the sum is the same. This makes sense because you are first filtered by the PK id_jovenclub and this record only has a 'natural' column.

I hope you gave me to understand and that you find the solution to your problem.

    
answered by 02.05.2017 / 22:16
source