Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

python - 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(). 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.

The Django docs don't help:

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)

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:

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?

question from:https://stackoverflow.com/questions/1545067/django-specify-which-model-manager-django-admin-should-use

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...