Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Created October 4, 2012 21:35
Show Gist options
  • Save glenrobertson/3836610 to your computer and use it in GitHub Desktop.
Save glenrobertson/3836610 to your computer and use it in GitHub Desktop.
Combine Django GeoManager with HStoreManager
# pip install django-orm-extensions
from django.contrib.gis.db.models.query import GeoQuerySet
from django.contrib.gis.db.models import GeoManager
from django_orm.postgresql.hstore.queryset import HStoreQuerySet
from django_orm.postgresql.hstore.manager import HStoreManager
class GeoHStoreQuerySet(GeoQuerySet, HStoreQuerySet):
pass
class GeoHStoreManager(models.GeoManager, HStoreManager):
def get_query_set(self):
return GeoHStoreQuerySet(self.model, using=self._db)
@fission6
Copy link

came up with this to that filter(some_field__contains) works

pip install django-orm-extensions

from django.contrib.gis.db.models import GeoManager
from django.contrib.gis.db.models.query import GeoQuerySet
from django.contrib.gis.db.models.sql.where import GeoWhereNode
from django.contrib.gis.db.models.sql import GeoQuery
from django_hstore.query import HStoreQuerySet, HStoreWhereNode
from django_hstore.managers import HStoreManager

class GeoHStoreWhereNode(GeoWhereNode, HStoreWhereNode):
pass

class GeoHStoreQuerySet(GeoQuerySet, HStoreQuerySet):

# change where node class
def __init__(self, model=None, query=None, using=None):
    super(GeoHStoreQuerySet, self).__init__(model=model,
        query=query, using=using)
    self.query = query or GeoQuery(self.model, where=GeoHStoreWhereNode)

def supports_services(self):
    """
    takes in a list of services, builds up a dict which makes to
    capabilities and queries the services hstore field to see which
    stations supports all requested services.
    """
    services_map = {
        "test": "Y",
    }

    return self.filter(services__contains=services_map)

class GeoHStoreManager(GeoManager, HStoreManager):

def get_query_set(self):
    return GeoHStoreQuerySet(self.model, using=self._db)

def supports_services(self):
    return self.get_query_set().supports_services()

def __getattr__(self, name):
    return getattr(self.get_query_set(), name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment