[Django]-Django filter for max difference between 2 columns

5👍

It should be something like:

from django.db.models import F, Max

RoundTable.objects
    .annotate(diff=F('total_seats')-F('occupied_seats'))
    .aggregate(Max('diff'))
👤Todor

1👍

Try this,

from django.db.models import Max, F, ExpressionWrapper, IntegerField

RoundTable.objects.annotate(diff=ExpressionWrapper(
    F('total_seats') - F('occupied_seats'), output_field=IntegerField()
)).aggregate(max=Max('diff'))
👤JPG

Leave a comment