[Answered ]-Django aggregate with expressions between ForeignKey (and not) values

1👍

You can work with:

from django.db.models import F, Sum

DrivingSession.objects.aggregate(
    total_liters=Sum(F('car__liter_per_km') * F('km'))
)

This will thus multiple the number of kilometers of that DrivingSession with the liter_per_km for that car.

Leave a comment