1

I am currently using DjangoREST for a server. When I want to do is to sort a queryset according to a custom function.

For an illustration a model could be like this:

class Product(models.Model):
    latitude = models.FloatField()
    longitude = models.FloatField()

In detail, I would like to send a request with my current geological coordinates and I would like the response to be sorted according to the distance between the product and myself.

I am using ModelSerializers and mixins if that is relevant.

The calculation is non-trivial which means it could not be accomplished by SQL sentences so annotate is not very useful in this case. And if I use:

queryset = Product.objects.all()
result = sort(queryset,lambda a: Somefunction(a))

The result is a list which is no longer a queryset, so I could not override the filter_queryset or relevant methods of DjangoREST filter class as they expect a queryset to be returned.

How could I accomplish this seemingly simple task with DjangoREST?

Lee
  • 11
  • 1

0 Answers0