Django Migrations Says Database Backend Isn't Available

落花浮王杯 提交于 2019-12-08 20:16:17

问题


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

回答1:


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




回答2:


Out of ideas, I upgraded Django from 1.7.10 to 1.8.4 and the problem went away. I don't know why this fixed it but it did.




回答3:


Proper fix is to install gdal-bin package (sudo apt-get install gdal-bin). For Xenial (16.04), it's already available in standard packages. For earlier versions, you might need to do these two additional steps before doing apt-get install:

sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update



回答4:


django.core.exceptions.ImproperlyConfigured: 
   'django.db.backends.postgresql_psycopg' is not an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
  'mysql', 'oracle', 'postgresql', 'sqlite3'

Well i did exactly what was told,changed the engine to:

"ENGINE":"django.db.backends.postgresql"

it works now.

(before this i tried sudo apt-get install gdal-bin which didn't work -> so if above solution doesn't work, you should do this too.)




回答5:


DATABASES in settings.py doesn't like capital letters (or perhaps postresql doesn't?).

I fixed this by changing capital letters to lowercase in my database 'NAME' field.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'MyDatabase',     <*****WRONG******
        'USER': 'admin',
        'PASSWORD': 'pass123',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Became:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',    <*****CORRECT******
        'USER': 'admin',
        'PASSWORD': 'pass123',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}


来源:https://stackoverflow.com/questions/32574472/django-migrations-says-database-backend-isnt-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!