I have a table named RoundTable and it has 2 fields, the max number of seats and the number of currently occupied seats. I want to find the max difference between these two columns.
My model:
class RoundTable(models.Model):
total_seats = models.SmallIntegerField(validators=[MinValueValidator(1)])
occupied_seats = models.SmallIntegerField(validators=[MinValueValidator(1)])
.... other fields
What would the query be like?
RoundTable.objects.aggregate(?)
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'))
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With