M
M
Michael Pak2015-07-06 12:12:22
PostgreSQL
Michael Pak, 2015-07-06 12:12:22

Should I use django.contrib.gis.db.backends.postgis for the entire project?

In a Django project , there are jobs that are attached to certain coordinates. It is necessary to implement sorting of tasks by distance from the user to the task. It was decided to use PostGIS for this , but after reading the documentation , it became clear that to work with PostGIS , you need to use the django.contrib.gis.db.backends.postgis engine :

DATABASES = {
    'default': {
         'ENGINE': 'django.contrib.gis.db.backends.postgis',
         'NAME': 'geodjango',
         'USER': 'geo',
     }
}

The question is: does it make sense to create a separate database that will support PostGIS and work through the engine described above? Or work with only one database and through one engine django.contrib.gis.db.backends.postgis ?
I understand what can be done with standard Postgres tools :
CREATE OR REPLACE FUNCTION distance(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS FLOAT AS $$
DECLARE                                                   
    x float = 69.1 * (lat2 - lat1);                           
    y float = 69.1 * (lon2 - lon1) * cos(lat1 / 57.3);        
BEGIN                                                     
    RETURN sqrt(x * x + y * y);                               
END
$$ LANGUAGE plpgsql;

but that won't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TitanFighter, 2016-03-02
@TitanFighter

There was a similar task. After reading everything you can, I did not find the answer. I decided to check myself how it will be with one base.
1. Under the geo data, I made a separate application app_geo.
2. I found on the internet that django.contrib.gis.db.models is inherited from django.db.models, so I wrote in this app in models.py

from django.contrib.gis.db import models

In the models of other applications left the old version
from django.db import models

3. Changed the engine in settings.py, as indicated in the header.
After migration, a couple of new tables from app_geo were added to the database.
In the end, everything works fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question