My code:
verloningsoverzicht_cumulatief_dagen =
Overzicht.objects.filter(periode=periode, jaar=jaar)
.values('testcontract', 'contract')
.annotate(aantal=Sum('aantal'))
Now lets say the .annotatie Sum is 6.3500000, i want it rounded to 6.35 The round method is not working, how can i solve this
.annotate(aantal=round(Sum('aantal'),2))
CodePudding user response:
You can make use of a Round
expression [Django-doc]:
from django.db.models.functions import Round
verloningsoverzicht_cumulatief_dagen = Overzicht.objects.filter(
periode=periode, jaar=jaar
) .values('testcontract', 'contract').annotate(
aantal=Round(Sum('aantal'), 2)
)
If you use django-3.2 or earlier, you can work with:
from django.db.models import DecimalField, ExpressionWrapper
from django.db.models.functions import Round
verloningsoverzicht_cumulatief_dagen = Overzicht.objects.filter(
periode=periode, jaar=jaar
) .values('testcontract', 'contract').annotate(
aantal=ExpressionWrapper(
Round(Sum('aantal') * 100) / 100,
output_field=DecimalField(max_digits=12, decimal_places=2)
)
)