Overwriting Manager in Django using django-cache-machine

梦想与她 提交于 2019-12-10 20:04:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!