Django 2.2.17 fails to paginate such a queryset because of `TypeError:
unhashable type: 'Distance'`:
{{{
#!python
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance
from django.core.paginator import Paginator
from django.db.models import Case, IntegerField, Q, Value, When
point = Point(0, 0)
cities = City.objects.annotate(
relative_distance=Case(
When(Q(location__distance_lte=(point, Distance(mi=100))),
then=Value(100)),
default=Value(1000),
output_field=IntegerField(),
),
)
Paginator(cities, 10).page(1)
}}}
This code works well with Django 1.11 and the latest development version.
For those who have faced this bug too, it is possible to use
`Distance(mi=100).m` that is a hashable float in the lookup.
I attached a file with a full exception taceback.
Thanks,
Illia
--
Ticket URL: <https://code.djangoproject.com/ticket/32358>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "traceback.txt" added.
* version: 2.2 => master
* easy: 0 => 1
* stage: Unreviewed => Accepted
Comment:
`MeasureBase` is clearly missing a `__hash__` method.
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:1>
Comment (by Mariusz Felisiak):
`Paginator` issue is fixed, but you can still encounter this issue when
grouping by annotation with `Distance()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:2>
* has_patch: 0 => 1
* stage: Accepted => Ready for checkin
Comment:
[https://github.com/django/django/pull/13902 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:3>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"bef6f7584280f1cc80e5e2d80b7ad073a93d26ec" bef6f758]:
{{{
#!CommitTicketReference repository=""
revision="bef6f7584280f1cc80e5e2d80b7ad073a93d26ec"
Fixed #32358 -- Fixed queryset crash when grouping by annotation with
Distance()/Area().
Made MeasureBase hashable.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:4>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"8dcb8b9e95eee0edb8e11ed02efb0f09a578b339" 8dcb8b9]:
{{{
#!CommitTicketReference repository=""
revision="8dcb8b9e95eee0edb8e11ed02efb0f09a578b339"
[3.2.x] Fixed #32358 -- Fixed queryset crash when grouping by annotation
with Distance()/Area().
Made MeasureBase hashable.
Backport of bef6f7584280f1cc80e5e2d80b7ad073a93d26ec from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:5>
Comment (by Illia Volochii):
Thanks, Mariusz. Could you please backport the commit to 2.2.x?
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:6>
Comment (by Tim Graham):
Per our [https://docs.djangoproject.com/en/dev/internals/release-process
/#supported-versions supported versions policy], 2.2 is only receiving
fixes for security and data loss issues.
--
Ticket URL: <https://code.djangoproject.com/ticket/32358#comment:7>