问题
Trying to run a GeoDjango app on Heroku and it's being a really piece of work. After struggling through a variety of problems, I've come to one I can't seem to hack my way out of.
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 673, in db_parameters
type_string = self.db_type(connection)
File "/app/.heroku/python/lib/python3.6/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'
I have properly used dj-database-url
to set the engine, however I'm still seeing the error
I added a print statement to output my db setups as they're being interpreted by py
In settings.py
:
if os.getenv('DYNO'):
GDAL_LIBRARY_PATH = os.path.expandvars(os.getenv('GDAL_LIBRARY_PATH'))
GEOS_LIBRARY_PATH = os.path.expandvars(os.getenv('GEOS_LIBRARY_PATH'))
DATABASES['default'] = dj_database_url.parse(os.getenv('DATABASE_URL'),'django.contrib.gis.db.backends.postgis')
print(DATABASES['default'])
Here is the print statement outputting what the heroku server is interpreting as my DATABASES['default']
credentials
{'NAME': 'name', 'USER': 'usr', 'PASSWORD': 'pw', 'HOST': 'herokuec2host.amazonaws.com', 'PORT': 5432, 'CONN_MAX_AGE': 0, 'ENGINE': 'django.contrib.gis.db.backends.postgis'}
Any help appreciated. I'm aware of the various similar stackoverflow posts regarding this issue, however all of the solutions I've researched have not solved the issue -- so asking again.
回答1:
Geodjango need to connect "postgis://..." but heroku change db settings to "postgres://". So, if you move DATABASES variable to bottom in settings.py, problem has been resolved. Happy hacking :)
django_heroku.settings(locals())
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': '*',
'USER': '*',
'PASSWORD': '*',
'HOST': '*',
'PORT': '5432',
},
}
来源:https://stackoverflow.com/questions/49969950/running-postgis-with-django-on-heroku