问题
Since the django-cache-machine override the objects model attribute, the right way to modify the queryset is as follows:
class ModelAQuerySet(models.query.QuerySet):
def foo(self):
return self.filter(name='foo')
class ModelAManager(base.CachingManager):
def __init__(self):
super(ModelAManager, self).__init__()
def get_query_set(self):
return ModelAQuerySet(self.model, using=self._db)
def foo(self):
return self.get_query_set().foo()
class ModelA(base.CachingMixin, models.Model):
#fields ...
objects = ModelAManager()
We can do objects = base.CachingManager
in some model. That means, the CachingManager
already inherits from models.Manager
itself. So we just create a model that inherits from base.CachingManager. Right?
Is this the right way to do it?
来源:https://stackoverflow.com/questions/16698084/overwriting-manager-in-django-using-django-cache-machine