Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - specify which model manager Django admin should use

Tags:

python

django

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?

like image 787
Chris Lawlor Avatar asked Oct 09 '09 17:10

Chris Lawlor


People also ask

Do you know about model Manager in Django can we write custom Manager how and why?

You can use a custom Manager in a particular model by extending the base Manager class and instantiating your custom Manager in your model. There are two reasons you might want to customize a Manager : to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.

What is default Manager in Django?

The default Manager object that Django provides in models is “objects“. Here “objects” is the manager object that Django creates by default. The 'create' method creates a new object. It takes field values that the model class wants.

What database does Django admin use?

Django, by default, uses SQLite3 for development. SQLite3 is a simple relational database engine and your database is automatically created as db. sqlite3 the first time you run python manage.py migrate .


1 Answers

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 
like image 193
Daniel Roseman Avatar answered Sep 18 '22 05:09

Daniel Roseman