Django ORM: how to cast geometry to geography in filter?

79 Views Asked by At

In my Django project, I have defined a geometry field on one of my model. Its SRID is 4326 (WGS 84).

I would like to write with Django ORM a query that will translate to:

select * from building
where
    (ST_DWITHIN(shape::geography,
    ST_MakePoint(1.9217,47.8814)::geography,20))

In other words, I would like to filter the queryset using the ST_DWITHIN function, applied to a geometry field (shape) casted to a geography.

The only way I could find is to use the extra() function, like so:

Building.objects.extra(
where=[
  f"ST_DWITHIN(shape::geography, ST_MakePoint({lng}, {lat})::geography, {radius})"
])
        

But the extra function is supposed to be deprecated in future version of Django, according to the doc.

Can I write this query without escaping from the ORM?

Maybe using the Cast function ?

0

There are 0 best solutions below