-
-
Save fission6/3902129 to your computer and use it in GitHub Desktop.
Combine Django GeoManager with HStoreManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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