Hi,
I am trying to write a custom lookup to use a PostGIS function inside a Django query. The function is st_x(point) that returns the x value of a point, which I want to compare with a user’s value. This works at a database level:
select point_z
from public.locations
where st_x(point_z) > 411073
This is the lookup I’m struggling with (I had never tried to create a custom lookup before, so I started from the example in the documentation):
@PointField.register_lookup
class XGt(GISLookup):
"""
Point X value greater than
"""
lookup_name = 'x_gt'
def as_sql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
query = 'st_x(%s) > %s' % (lhs, rhs)
return query, params
When I try to use it I always get the following error message:
ValueError: Cannot use object with type int for a spatial lookup parameter.
I think that at some point in /lib/python3.8/site-packages/django/db/models/query.py all spatial lookups are supposed to have spatial objects on left hand and right hand side, but not this specific lookup…
Any help would be appreciated
PS: I am using Django 3.1