Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29422 closed Cleanup/optimization (invalid)

QuerySet.extra use case - "earth_box", "ll_to_earth"

Reported by: Mateusz Kurowski Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords: QuerySet.extra, queryset, extra, earth_box, ll_to_earth, models, function, expression, manager
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

https://docs.djangoproject.com/en/2.0/ref/models/querysets/#extra

This is my use case:

Code highlighting:

class City(models.Model):
    longitude = models.FloatField(db_index=True)
    latitude = models.FloatField(db_index=True)

# this works:
City.objects.extra(where=['earth_box(ll_to_earth(%s,%s), %s) @> ll_to_earth(latitude, longitude)'], params=[51.41816, 15.90689, 500000])
# this does not:
City.objects.extra(where=['earth_box(ll_to_earth(%s,%s), %s) @> ll_to_earth(latitude, longitude)'], params=[F('latitude'), F('longitude'), 50000])
# psycopg2.ProgrammingError: can't adapt type 'F'

I could rewrite this in a custom Manager, but how?

Change History (2)

comment:1 by Shai Berger, 6 years ago

Resolution: invalid
Status: newclosed

Without going into too many details, I'd expect you to be able to define two user functions (something like LLToBox() taking two arguments and LLToEarthBox() taking three) and use the builtin contains comparison, or, if this doesn't work, define your own comparison.

In general, please direct such questions to the available help channels. I am closing this ticket as invalid, for more details see TicketClosingReasons/UseSupportChannels.

comment:2 by Mateusz Kurowski, 6 years ago

@Shai Berger how i can use contains? Can you show an example queryset, how you see it?

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