Django - specify which model manager Django admin should use

前端 未结 3 1922
温柔的废话
温柔的废话 2020-12-04 23:47

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

3条回答
  •  爱一瞬间的悲伤
    2020-12-05 00:25

    You can choose the manager by overriding the queryset method in your ModelAdmin subclass.

    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
    

提交回复
热议问题