我使用 django-model-utils因为它是 InheritanceManager 类,所以我的父模型 Activity
的查询集也从它们的子类返回信息,如 Sports
、Party
等。
但是,现在我想创建一个自定义查询以默认添加一些过滤器,但我无法让它在 InheritanceManager 中工作。由于 InheritanceManager 已经替换了我模型的默认管理器,因此我无法创建自定义管理器,如 Django's docs 所述.
简单地从 InheritanceManager 继承自定义管理器是行不通的。还有其他想法吗?
最佳答案
当然可以,只需定义您自己的继承自 InheritanceManager
的自定义管理器类,如下所示:
from model_utils.managers import InheritanceManager
class CustomInheritanceManager(InheritanceManager):
def get_queryset(self):
# your custom queryset here ...
class YourModel(models.Model):
objects = CustomInheritanceManager()
关于Django 模型实用程序 : Use InheritanceManager with custom querysets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985137/