Created
December 5, 2013 13:33
-
-
Save rodrigoolmo/7805193 to your computer and use it in GitHub Desktop.
If you use GeoDjango your models require a GeoManager model manager.
If you use django-polymorphic for inheritance in Django your models require a PolymorphicManager model manager.
What if you need both functionalities? Then use this manager: GeoPolymorphicManager
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
from django.contrib.gis.db import models | |
from django.contrib.gis.db.models.query import GeoQuerySet | |
from polymorphic import PolymorphicManager | |
from polymorphic.query import PolymorphicQuerySet | |
class GeoPolymorphicQuerySet(GeoQuerySet, PolymorphicQuerySet): | |
''' | |
QuerySet used in GeoPolymorphicManager. | |
''' | |
pass | |
class GeoPolymorphicManager(models.GeoManager, PolymorphicManager): | |
''' | |
GeoManager with polymorfism functionalities (for django-polymorphic). | |
''' | |
queryset_class = GeoPolymorphicQuerySet | |
def get_query_set(self): | |
return self.queryset_class(self.model, using=self._db) |
Good morning, fellow googlers.
The above solution doesn't work (any more). I've modified it slightly, and also added a base model, for simplicity.
from django.contrib.gis.db.models.query import GeoQuerySet
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicModel
from polymorphic.query import PolymorphicQuerySet
class GeoPolymorphicQuerySet(GeoQuerySet, PolymorphicQuerySet):
pass
class GeoPolymorphicManager(models.GeoManager, PolymorphicManager):
"""
GeoManager with polymorphism functionalities (for django-polymorphic).
"""
queryset_class = GeoPolymorphicQuerySet
def get_query_set(self):
return self.queryset_class(self.model, using=self._db)
class Model(PolymorphicModel):
objects = GeoPolymorphicManager()
class Meta:
abstract = True
This does not work anymore:
$ pip freeze | grep -i django
Django==2.1.3
django-bootstrap3==11.0.0
django-debug-toolbar==1.10.1
django-geojson==2.11.0
django-leaflet==0.24.0
django-polymorphic==2.0.3
$ python -c 'from django.contrib.gis.db.models.query import GeoQuerySet'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'django.contrib.gis.db.models.query'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple and effective.