Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Support for PostGIS backend optionally #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

diranged
Copy link

Optionally support django.contrib.gis.db.backends.postgis as the DB backend

@KristianOellegaard
Copy link

Wouldn't it be better to implement a setting where you specify your BaseDatabaseWrapper, DjangoCursorWrapper and BaseDatabaseCreation of choice? Then this would work with django-hstore and more, as well.

@kennethreitz
Copy link
Contributor

+1, Mozilla's MySQL pool works this way. I would love to implement that.

@diranged
Copy link
Author

So I wanted to do that originally, but I found if I tried to import any DatabaseWrappers inside the Django settings file, I run into this error:

from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper
Traceback (most recent call last):
File "", line 1, in
File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 1, in
from django.db.backends.postgresql_psycopg2.base import *
File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/db/init.py", line 11, in
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/conf/init.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

@KristianOellegaard
Copy link

You could just write a string in the settings file and then do the import in the module, based in the settings value ;)

e.g.

DATABASE_POOL_CONFIG = {
    'BaseDatabaseWrapper': 'some.path.to.BaseDatabaseWrapper',
    'DjangoCursorWrapper': 'some.path.to.DjangoCursorWrapper',
    'BaseDatabaseCreation': 'some.path.to.BaseDatabaseCreation'
}

@diranged
Copy link
Author

I tried something like this:

DEFAULT_POOL_CONFIG = {
    'BaseDatabaseWrapper': 'django.contrib.gis.db.backends.postgis.base.DatabaseWrapper',
    'DjangoCursorWrapper': 'django.contrib.gis.db.backends.postgis.base.CursorWrapper',
    'BaseDatabaseCreation': 'django.contrib.gis.db.backends.postgis.creation.DatabaseCreation',
}

def _import_class(cl):
    d = cl.rfind(".")
    classname = cl[d + 1:len(cl)]
    m = __import__(cl[0:d], globals(), locals(), [classname])
    return getattr(m, classname)


# Look up the DATABASE_POOL_CONFIG settings. If they are supplied, use the to
# dynamically import the objects we're going to wrap. If they are not set, use
# the defaults configured at the top of this package.
pool_config = getattr(settings, 'DATABASE_POOL_CONFIG', DEFAULT_POOL_CONFIG)
BaseDatabaseWrapper = _import_class(pool_config['BaseDatabaseWrapper'])
DjangoCursorWrapper = _import_class(pool_config['DjangoCursorWrapper'])
BaseDatabaseCreation = _import_class(pool_config['BaseDatabaseCreation'])

but I end up with errors because you refer to many components of django.contrib.gis.db.backends.postgis.base by importing *..

(nextdoor-ve)diranged@mwise-x64-dev:~/src/nextdoor.com/apps/nextdoor$ python manage.py shell
Traceback (most recent call last):
  File "manage.py", line 6, in <module>
    execute_manager(settings)
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/core/management/__init__.py", line 459, in execute_manager
    utility.execute()
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
  File "/home/diranged/src/nextdoor.com/apps/nextdoor/../nextdoor/web/management/commands/shell.py", line 2, in <module>
    from nextdoor.web import signals
  File "/home/diranged/src/nextdoor.com/apps/nextdoor/../nextdoor/web/signals.py", line 14, in <module>
    from django.contrib.auth.models import User
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/contrib/auth/models.py", line 5, in <module>
    from django.db import models
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
    return import_module('.base', backend_name)
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/diranged/src/nextdoor.com/apps/nextdoor/../nextdoor/db/backend/base.py", line 21, in <module>
    from django_postgrespool import base as database_base
  File "/opt/nextdoor-ve/lib/python2.7/site-packages/django_postgrespool/base.py", line 46, in <module>
   db_pool = manage(Database, **pool_args)
NameError: name 'Database' is not defined

@MRigal
Copy link

MRigal commented Oct 22, 2013

I have actually a big + one for this !

@gonzalodiaz
Copy link

Was the support for django.contrib.gis.db.backends.postgis added elsewhere?

@cmcavoy
Copy link

cmcavoy commented Feb 4, 2015

also interested in where this landed....is there a way to use postgis with this library?

@Starefossen
Copy link

+1 for being able to use PostGIS with Django-PostgresPool

@leibowitz
Copy link

Still no support/planned support for postgis in postgrespool?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

9 participants