注释字段的Django Queryset绝对值 [英] Django Queryset absolute value of the annotated field
本文介绍了注释字段的Django Queryset绝对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取带注释字段的绝对值?我尝试了下面的代码,但没有用。
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')
错误:
TypeError:abs()的操作数类型错误:'CombinedExpression'
提前谢谢!
推荐答案
您可以尝试使用 func表达式:
from django.db.models import Func, F
queryset.annotate(relevance=Func(F('capacity') - int(request.GET['capacity']), function='ABS'))
这篇关于注释字段的Django Queryset绝对值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文