I have a Django application that needs to work with an existent PostGIS database, apart from its own database. I've configured multidatabase support, created a database router, created models for the existent tables in the PostGIS database and told syncdb that, for that models, managed=False (so, it won't create the tables in the database, they already exist)

My problem comes because I need to create distance queries using one of those PostGIS tables. The table stores points objects with srid 4326, and I need to know how many of these points are within a given distance of a reference point (like ST_DWithin).

I'd like to use geography for the distance queries. So, I could define the geom field in the model that maps to that table with geography=True. But the problem is I just have read access to the PostGIS database. So, I can't change the column type to geography. I don't want to write a raw query, transforming the column to geography. Unless there's no other method. Is there a geodjango-friendly way to deal with this?


Can you not define a view in the PostGIS DB and then add that view into a separate model in Django?

For example:

Geography(ST_Transform(geom,4326)) AS geog,
FROM table;

Then you can just use the model as normal. Admittedly this might confuse your model code.

  • I got write access to the table, and created the geography field. Anyway, that solution worked too. Many thanks! – Jorge Arévalo Feb 4 '14 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.