Answer the question
In order to leave comments, you need to log in
How to annotate a Django calculated field?
You need to sort the data array by distance from the current location. Objects have a reference to an address that has a longitude and latitude. I'm trying to create additional fields by calculating with the formula:
√ ((x A - x B) 2 + (y A - y B) 2)
queryset.annotate(distance=((longitude - F("address__longitude")) * 2) + ((latitude - F("address__latitude")) * 2)).order_by("distance")
Expression contains mixed types: FloatField, DecimalField. You must set output_field.
but I don't know where to add this attribute. Answer the question
In order to leave comments, you need to log in
from django.db.models import ExpressionWrapper, F
queryset.annotate(
distance=ExpressionWrapper(
expression=((1 - F("address__longitude")) * 2) + ((1 - F("address__latitude")) * 2),
output_field=models.FloatField,
),
) \
.order_by("distance")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question