Skip to content

Instantly share code, notes, and snippets.

@fission6
Forked from glenrobertson/geo_hstore_manager.py
Created October 16, 2012 21:22
Show Gist options
  • Save fission6/3902129 to your computer and use it in GitHub Desktop.
Save fission6/3902129 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 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