Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Django order by Min Distance to a list of points

I want to get a queryset of all Jobs that are within a given distance to at least one of the many provided locations, order them by the minimum distance, and do not show duplicate jobs.

from django.db import models
from cities.models import City    

class Job(models.Model):
    title = models.CharField(max_length=255)
    cities = models.ManyToManyField(City)

If there is only one point I could do this as:

from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import Point
point = Point(x, y, srid=4326)  
Job.objects.filter(cities__location__dwithin=(point, dist)) \
           .annotate(distance=Distance("cities__location", point) \
           .order_by('distance')

but when I have many points I build out a Q expression for the filter but am unsure of a clean way to annotate the Min distance of the Job to all points

query = Q()
for point in points:
    query |= Q(cities__location__dwithin=(point, dist))
Job.objects.filter(query).annotate(distance=Min(...)).order_by('distance')

FYI using postgres 12.1 with PostGIS extension

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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 is an aggregation function that takes a single expression, such as a column name, and reduces multiple inputs to a single output value.
  • LEAST is a conditional expression that works by selecting the smallest value from a list of any number of expressions

https://docs.djangoproject.com/en/3.0/ref/models/querysets/#min https://docs.djangoproject.com/en/3.0/ref/models/database-functions/#minus

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda