1

I'm using Cache Machine's Cache Manager on my non-geographic models using the pattern in the docs:

from django.db import models

from caching.base imoprt CachingManager, CachingMixin

class Zomg(CachingMixin, models.Model):
    val = models.IntegerField()

    objects = CachingManager()

But I have several models containing GeoDjango field types, and therefore must use GeoManager, e.g.

class RecordArea(models.Model):
    polygon = models.MultiPolygonField(srid=4326)
    name = models.CharField(max_length=100)
    ...
    objects = models.GeoManager()

How can I integrate these two managers on my geographic models? I'm on Django 1.5 / Python 2.7.5.

This points to overriding CachingManager, which I get, but the Cache Machine docs make me think I need to make sure the QuerySet gets cached, i.e. becomes a CachingQuerySet:

return a CachingQuerySet from the other manager’s get_query_set method instead of subclassing CachingManager

Community
  • 1
  • 1
Kim
  • 185
  • 8

1 Answers1

2

Make your own custom manager that inherits from GeoManager and returns a CachingQuerySet:

In myapp/manager.py:

from django.contrib.gis.db.models import GeoManager
from caching.base import CachingQuerySet

class MyModelManager(GeoManager):
    """
    A custom manager for myapp models.
    """
    def get_queryset(self):
        return CachingQuerySet(self.model, using=self._db)

In myapp/models.py:

from django.contrib.gis.db import models
from caching.base import CachingMixin
from .manager import MyModelManager

class MyModel(CachingMixin, models.Model):
    something = models.CharField()

    objects = MyModelManager()

And you got yourself a cacheable model.

Amir Rustamzadeh
  • 4,302
  • 7
  • 35
  • 43