How to access the geometry field (points) in a PostGIS database from Django?

In my project, I use PostgreSQL / PostGIS as the database and Django with django.contrib.gis. the existing table pois contains geospatial point data . Here is an excerpt from the SQL create statement:

CREATE TABLE pois
(
  ogc_fid serial NOT NULL,
  the_geom geometry(Point,900914),
  name character varying(254),
  -- ...

I created a Django model with the following command:

$ python manage.py inspectdb

The generated model is as follows:

from django.contrib.gis.db import models

class POIs(models.Model):
    ogc_fid = models.AutoField(primary_key=True)
    the_geom = models.TextField(blank=True, null=True) # This field type is a guess.
    name = models.CharField(max_length=254, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'pois'

I add the following field to overwrite the default control with a specific instance of GeoDjango:

objects = models.GeoManager()

Now I want to replace the_geom = models.TextField()with the correct data type , which should be models.PointField()or models.GeometryField(). I tried both. Then I test the model in the Python shell:

$ python manage.py shell
Python 3.4.3 (default, Jul 28 2015, 18:20:59) 
In [1]: from berlin import models
In [2]: models.POIs.objects.first()

This fails and the following stacktrace is issued :

/home/user/.virtualenvs/myproject/lib/python3.4/site-packages/django/contrib/ \
    gis/db/models/fields.py in select_format(self, compiler, sql, params)
     57         else:
     58             sel_fmt = '%s'
---> 59         if connection.ops.select:
     60             # This allows operations to be done on fields in the SELECT,
     61             # overriding their values -- used by the Oracle and MySQL

AttributeError: 'DatabaseOperations' object has no attribute 'select'

models.TextField . .

+2
1

settings.py :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'geodjango',
        'USER': 'geo',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis', # Here
        'NAME': 'geodjango',
        'USER': 'geo',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

-. apollo13 #django irc .

+1

Source: https://habr.com/ru/post/1502579/