Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

postgresql - Django Migrations Says Database Backend Isn't Available

I'm trying to upgrade a Django 1.6.2 application to 1.7.10. I'm running PostgreSQL on my Mac using the Postgres.app version 9.3.4 that runs PostgreSQL 9.3.4 and PostGIS 2.1.1. The problem I'm having is that when I run the new "makemigrations" command, I'm getting the following error (the entire stacktrace is at the bottom):

django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.postgis' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseSpatialOperations

Here are the pertinent settings:

# conf/settings/base.py
DATABASES = {
    'default': {
        'ENGINE'  : 'django.contrib.gis.db.backends.postgis',
        'NAME'    : 'geodb',
        'USER'    : 'geo',
        'PASSWORD': 'geopassword',   # not really
        'HOST'    : 'localhost',
        'PORT'    : '',
    }
}
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.gis',
    'apps.admin',  # my apps live in an apps subdir.
    'apps.home',
)
POSTGIS_VERSION = (2,1,1)

I used this same DATABASE ENGINE setting in my Django 1.6.2 application without any problems. In researching this, some other developers forgot to install psycopg2 in their virtual environment but I do have version 2.6.1 installed in mine. I was using version 2.5.2 with Django 1.6.2 but if I roll psycopg2 back to that version I still get this error. I've read all the psycopg2 release notes between those two versions and didn't see anything that might cause this problem.
The Django 1.7 GeoDjango tutorial also uses this postgis engine setting so it doesn't appear to be deprecated and there isn't anything pertinent in the Django 1.7 release notes that indicate this could be a problem.

I also built a test version of the GeoDjango tutorial mentioned above and if I run the makemigrations command on it, I get the very same error.

Finally, I've done a web search and don't see any articles that discuss this migrations/database engine problem. Does anyone see what's wrong?

Thanks!

# Full stacktrace
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
    class Permission(models.Model):
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/db/models/base.py", line 297, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/me/venv/dj_17/lib/python2.7/site-packages/django/db/utils.py", line 126, in load_backend
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.postgis' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseSpatialOperations
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was having similar problems upgrading from 1.9.9 to 1.10.1

django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.postgis' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: cannot import name 'GDALRaster'

It went away by upgrading the gdal dependency -> brew install --upgrade gdal for me on OSX


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...