简体   繁体   English

Django - 指定Django管理员应该使用哪个模型管理器

[英]Django - specify which model manager Django admin should use

I've created a custom Manager for a Django model which returns a QuerySet holding a subset of objects.all(). 我为Django模型创建了一个自定义管理器,它返回一个包含objects.all()子集的QuerySet。 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 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遇到(按照它们在模型中定义的顺序)具有特殊状态。 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将类中定义的第一个Manager解释为“默认”管理器,Django的几个部分( 尽管不是管理应用程序 )将专门为该模型使用该Manager。 (Django Managers documentation) (Django Managers文档)

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?

You can choose the manager by overriding the queryset method in your ModelAdmin subclass. 您可以通过覆盖ModelAdmin子类中的queryset方法来选择管理器。

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

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可以替换......

As we expect objects to be the sole manager, the admin will use manager in self.Admin.manager . 由于我们希望objects成为唯一的管理者,因此管理员将在self.Admin.manager使用manager

From the ticket https://code.djangoproject.com/ticket/4754 opened 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测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM