[Django] #32358: Paginating a queryset with a distance lookup fails due to unhashable type Distance

30 views
Skip to first unread message

Django

unread,
Jan 16, 2021, 7:44:23 AM1/16/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
------------------------------------------+--------------------------
Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 2.2
Severity: Normal | Keywords: distance
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+--------------------------
There is a model with a `location = models.PointField()` field.

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.

Django

unread,
Jan 16, 2021, 7:45:05 AM1/16/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
--------------------------------+--------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 2.2
Severity: Normal | Resolution:

Keywords: distance | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Illia Volochii):

* Attachment "traceback.txt" added.

Django

unread,
Jan 16, 2021, 8:25:51 AM1/16/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
--------------------------------+------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: master
Severity: Normal | Resolution:
Keywords: distance | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Claude Paroz):

* 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>

Django

unread,
Jan 18, 2021, 3:14:32 AM1/18/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
--------------------------------+------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: master
Severity: Normal | Resolution:
Keywords: distance | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------+------------------------------------

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>

Django

unread,
Jan 18, 2021, 4:00:47 AM1/18/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
-------------------------------------+-------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: master
Severity: Normal | Resolution:
Keywords: distance | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Jan 18, 2021, 6:07:25 AM1/18/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
-------------------------------------+-------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: closed
Component: GIS | Version: master
Severity: Normal | Resolution: fixed

Keywords: distance | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Django

unread,
Jan 18, 2021, 6:07:58 AM1/18/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
-------------------------------------+-------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: closed
Component: GIS | Version: master
Severity: Normal | Resolution: fixed
Keywords: distance | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 18, 2021, 7:38:50 AM1/18/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
-------------------------------------+-------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: closed
Component: GIS | Version: master
Severity: Normal | Resolution: fixed
Keywords: distance | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 18, 2021, 9:18:40 AM1/18/21
to django-...@googlegroups.com
#32358: Paginating a queryset with a distance lookup fails due to unhashable type
Distance
-------------------------------------+-------------------------------------

Reporter: Illia Volochii | Owner: nobody
Type: Bug | Status: closed
Component: GIS | Version: master
Severity: Normal | Resolution: fixed
Keywords: distance | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages