Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32358 closed Bug (fixed)

Paginating a queryset with a distance lookup fails due to unhashable type Distance

Reported by: Illia Volochii Owned by: nobody
Component: GIS Version: dev
Severity: Normal Keywords: distance
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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':

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

Attachments (1)

traceback.txt (9.6 KB ) - added by Illia Volochii 4 years ago.

Download all attachments as: .zip

Change History (8)

by Illia Volochii, 4 years ago

Attachment: traceback.txt added

comment:1 by Claude Paroz, 4 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted
Version: 2.2master

MeasureBase is clearly missing a __hash__ method.

comment:2 by Mariusz Felisiak, 4 years ago

Paginator issue is fixed, but you can still encounter this issue when grouping by annotation with Distance().

comment:3 by Mariusz Felisiak, 4 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: newclosed

In bef6f758:

Fixed #32358 -- Fixed queryset crash when grouping by annotation with Distance()/Area().

Made MeasureBase hashable.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 8dcb8b9:

[3.2.x] Fixed #32358 -- Fixed queryset crash when grouping by annotation with Distance()/Area().

Made MeasureBase hashable.

Backport of bef6f7584280f1cc80e5e2d80b7ad073a93d26ec from master

comment:6 by Illia Volochii, 4 years ago

Thanks, Mariusz. Could you please backport the commit to 2.2.x?

comment:7 by Tim Graham, 4 years ago

Per our supported versions policy, 2.2 is only receiving fixes for security and data loss issues.

Note: See TracTickets for help on using tickets.
Back to Top