GeoDjango with Docker error: 'DatabaseOperations' object has no attribute 'geo_db_type'

GeoDjango with Docker error: 'DatabaseOperations' object has no attribute 'geo_db_type'

这似乎是一个常见的错误,我已经检查了所有我能找到的解决方案(只有大约 4 个,几乎所有的都涉及错误配置)。我没有使用 heroku,但我正在使用 docker。我正在使用 docker 图像 python:3.9.7postgis/postgis:10-3.1-alpine。 我的 Dockerfile 包含以下行:

ARG BUILD_ENV=production
# ...
FROM python:3.9.7 as apiserver
#...
RUN apt-get update && \
    apt-get install --no-install-recommends -y apt-utils build-essential sudo git wget curl ca-certificates nginx openssl libpq-dev expect libmagic-dev graphviz python3-gdal python3-pandas postgis binutils libproj-dev gdal-bin python3-djangorestframework-gis
# ...
RUN pip install --upgrade pip && pip install -r requirements/${BUILD_ENV}.txt
# ...

这应该确保我为 django 构建 postgis 库所需的所有依赖项都存在,所以当我 运行 pip 安装时,它将拥有它需要的一切。它比需要的更冗长,因为 GeoDjango 文档说要安装 postgis binutils libproj-dev gdal-bin,而 drf-gis 将所有 GeoDjango 库作为依赖项。我计划使用 drf-gis 包,但目前没有使用它。

requirements/production.txt

django-cors-headers==3.5.0
django-extensions==3.0.9
django-filter==2.4.0
# ...
Django==3.1.13
# ...
djangorestframework==3.12.4
djangorestframework-gis==0.17
# ...

我在 INSTALLED_APPS 中启用了正确的应用程序:

INSTALLED_APPS = [
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',
    'django_extensions',
    'rest_framework',
    'rest_framework.authtoken',
    # 'rest_framework_gis',
    'django.contrib.sites',
    'django_filters',
    'apiserver',  # api v1 app doesn't use geodjango
    'apiserver_v2', # api v2 uses geodjango
    # ...
]

我还将以下内容设置为我的数据库配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'HOST': os.environ.get('PGHOST'),
        'PORT': 5432,
        'NAME': os.environ.get('PGDATABASE'),        
        'USER': os.environ.get('PGUSER'),
        'PASSWORD': os.environ.get('PGPASSWORD'),
        'CONN_MAX_AGE': os.environ.get('DB_CONN_MAX_AGE', 500),
    },
}

我的应用程序中还有以下迁移作为第一个迁移。我在使用 SpatiaLite 进行测试时不需要它,但我将它添加到此处以完善文档。

0000_postgis.py

from django.contrib.postgres.operations import CreateExtension
from django.db import migrations


class Migration(migrations.Migration):

    operations = [
        CreateExtension('postgis'),
        CreateExtension('postgis_raster'),
    ]

此迁移按预期工作,并且似乎已安装扩展:

apiserver_db_name=# \dx
                                    List of installed extensions
      Name      | Version |   Schema   |                        Description                         
----------------+---------+------------+------------------------------------------------------------
 plpgsql        | 1.0     | pg_catalog | PL/pgSQL procedural language
 postgis        | 3.1.4   | public     | PostGIS geometry and geography spatial types and functions
 postgis_raster | 3.1.4   | public     | PostGIS raster types and functions
(3 rows)

现在已经根据文档进行了所有设置。但是,当我 运行 我的迁移时,出现以下错误:

Operations to perform:
  Apply all migrations: admin, apiserver, apiserver_v2, auth, authtoken, contenttypes, enter, guardian, sessions, sites
Running migrations:
  Applying apiserver_v2.0000_postgis... OK
  Applying apiserver_v2.0001_initial...Traceback (most recent call last):
  File "/api-server/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 322, in create_model
    sql, params = self.table_sql(model)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 159, in table_sql
    definition, extra_params = self.column_sql(model, field)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 212, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/fields/__init__.py", line 717, in db_parameters
    type_string = self.db_type(connection)
  File "/usr/local/lib/python3.9/site-packages/django/contrib/gis/db/models/fields.py", line 105, in db_type
    return connection.ops.geo_db_type(self)
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

特别说明:我已经查看了所有我能找到的导致此错误的问题,但我找不到任何关于该错误实际含义的解释。为什么要查找此属性,我如何验证它是否具有此属性?什么包裹应该放在那里以及如何放置?如果我能找到它,诊断和修复这个问题就会容易得多。

更新:我用一个干净的 postgis 数据库试过这个,但得到了同样的错误。我还尝试使用 pg_dumped 和 pg_restored 数据库。运气不好。

已解决!这是由于另一个配置覆盖了数据库设置。我非常坚定地认为这不是配置问题,我很抱歉。我通过 运行 管理员 shell 在 运行 django 应用程序中验证了这一点并检查了 settings.DATABASES。 (另一个覆盖是 django-prometheus,如果你好奇的话。)