Untitled

mail@pastecode.io avatar
unknown
python
14 days ago
1.0 kB
2
Indexable
Never
doctors.exclude(
                Q(address__latitude__isnull=True) & Q(
                    address__longitude__isnull=True)
            ).annotate(
                distance=ExpressionWrapper(
                    6371 * 2 * ATan2(
                        Sqrt(
                            Sin(Radians(F('address__latitude') - cur_lat) / 2) ** 2 +
                            Cos(Radians(cur_lat)) * Cos(Radians(F('address__latitude'))) *
                            Sin(Radians(F('address__longitude') - cur_long) / 2) ** 2
                        ),
                        Sqrt(1 - (
                            Sin(Radians(F('address__latitude') - cur_lat) / 2) ** 2 +
                            Cos(Radians(cur_lat)) * Cos(Radians(F('address__latitude'))) *
                            Sin(Radians(F('address__longitude') - cur_long) / 2) ** 2
                        ))
                    ),
                    output_field=FloatField()
                )
            ).filter(distance__lte=float(near)).order_by("distance")
Leave a Comment