How do I get the absolute value of the annotated field? I tried the code below but it did't work.
queryset.annotate(relevance=abs(F('capacity') - int( request.GET['capacity']) ) ).order_by('relevance')
Error:
TypeError: bad operand type for abs(): 'CombinedExpression'
Thanks in advance!
You can try with func expressions:
from django.db.models import Func, F
queryset.annotate(relevance=Func(F('capacity') - int(request.GET['capacity']), function='ABS'))
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