Slide 25
Slide 25 text
from django.db.models import Q, Sum # Avg, Max, Min,
Variance
class DeputadoQuerySet(models.QuerySet):
# annotate deputy’s expenses by month/year
def annotate_gasto_no_mes_por_deputado(self, mes, ano):
annotation = {
f'gastos_{ano}_{mes:02}': Sum( # ‘gastos_2018_01’
'gastos__valor_liquido', # net value
filter=Q(
gastos__mes=mes, # month
gastos__ano=ano # year
)
)
}
return self.annotate(**annotation)