1 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個贊
query = Q()
distances = []
for point in points:
query |= Q(cities__location__dwithin=(point, dist))
distances.append(Distance("cities__location", point))
# LEAST requires 2 or more expressions, MIN works for single expression
if len(distances) == 1:
MIN_FUNC = Min
else:
MIN_FUNC = Least
Job.objects.filter(query).annotate(distance=MIN_FUNC(*distances)).order_by('distance')
MIN是一個聚合函數(shù),它采用單個表達(dá)式(例如列名)并將多個輸入減少為單個輸出值
LEAST是一個條件表達(dá)式,它通過從任意數(shù)量的表達(dá)式列表中選擇最小值來發(fā)揮作用
https://docs.djangoproject.com/en/3.0/ref/models/querysets/#min https://docs.djangoproject.com/en/3.0/ref/models/database-functions/#least
添加回答
舉報