python Django - 指定 Django 管理员应该使用哪个模型管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1545067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - specify which model manager Django admin should use
提问by Chris Lawlor
I've created a custom Manager for a Django model which returns a QuerySet holding a subset of objects.all(). I need this to be the model's default Manager, since I am also creating a custom tag which will retrieve content from any model (specified by an argument), and needs to use the default Manager for the specified model. All that works fine, except - Django Admin is ALSO using the default Manager for this particular model, which means that not all model instances appear in the admin.
我为 Django 模型创建了一个自定义管理器,它返回一个包含 objects.all() 子集的 QuerySet。我需要它作为模型的默认管理器,因为我还创建了一个自定义标签,它将从任何模型(由参数指定)检索内容,并且需要使用指定模型的默认管理器。一切正常,除了 - Django Admin 也使用此特定模型的默认管理器,这意味着并非所有模型实例都出现在 admin 中。
The Django docs don't help:
Django 文档没有帮助:
If you use custom Manager objects, take note that the first Manager Django encounters (in the order in which they're defined in the model) has a special status. Django interprets this first Manager defined in a class as the "default" Manager, and several parts of Django (though not the admin application) will use that Manager exclusively for that model. (Django Managers documentation)
如果您使用自定义管理器对象,请注意 Django 遇到的第一个管理器(按照它们在模型中定义的顺序)具有特殊状态。Django 将类中定义的第一个 Manager 解释为“默认”Manager,Django 的几个部分(虽然不是管理应用程序)将专门为该模型使用该 Manager。 (Django 管理器文档)
The admin isn't supposed to use the default Manager, but it seems to be in my case. Note that I have also explicitly add the default Manager objects
:
管理员不应该使用默认的管理器,但在我的情况下似乎是这样。请注意,我还明确添加了默认 Manager objects
:
subset = CustomManager() # the default manager
objects = models.Manager() # the one I want admin to use
How can I specify which Manager the admin should use?
如何指定管理员应该使用哪个 Manager?
回答by Daniel Roseman
You can choose the manager by overriding the queryset
method in your ModelAdmin subclass.
您可以通过覆盖queryset
ModelAdmin 子类中的方法来选择管理器。
def get_queryset(self, request):
# use our manager, rather than the default one
qs = self.model.objects.get_queryset()
# we need this from the superclass method
ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
if ordering:
qs = qs.order_by(*ordering)
return qs
回答by Alexander Ovchinnikov
Updated code:
更新代码:
def get_queryset(self, request):
"""
Returns a QuerySet of all model instances that can be edited by the
admin site. This is used by changelist_view.
"""
qs = self.model._default_manager.get_queryset()
# TODO: this should be handled by some parameter to the ChangeList.
ordering = self.get_ordering(request)
if ordering:
qs = qs.order_by(*ordering)
return qs
_default_manager can be replaced...
_default_manager 可以替换...
回答by Victor T
As we expect objects
to be the sole manager, the admin will use manager
in self.Admin.manager
.
由于我们希望objects
成为唯一的经理,因此管理员将使用manager
in self.Admin.manager
。
From the ticket https://code.djangoproject.com/ticket/4754opened by troy.simpson
来自troy.simpson 打开的票https://code.djangoproject.com/ticket/4754
class filterManager(models.Manager):
def get_query_set(self):
return super(filterManager, self).get_query_set().filter(name='troy')
class Blah(models.Model):
name = models.CharField(maxlength=100)
objects = filterManager()
class Admin:
manager = filterManager()
Tested with Django 1.11
用 Django 1.11 测试