You have a rather complicated setup, it would be easier to have an HDSVunits in the Price model to simplify access to queries.
The best I can come up with is something like this:
Booking.objects.aggregate( hdsv=( Sum('classnumbers__numberofstudents') * Case( When(price__name='Full-Day', then=2.0), When(price__name='Half-Day AM', then=1.0), When(price__name='Full-Day PM', then=1.0), When(price__name='Three-Quarter Day', then=1.5), When(price__name='1 Hour', then=0.5), When(price__name='Custom', then=1.0), output_field=FloatField(), ) ) )
If the HDSV value was saved as a field in the Price model, you could simply do:
Booking.objects.aggregate( hdsv=Sum('classnumbers__numberofstudents') * F('price__hdsv'))
On the side of the note, you should really consider the following Python naming conventions that would make it easier for other Python developers to help you.
Villiers strauss
source share