Manager Documentation and Admin interface
|Reported by:||mm||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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. As a result, it's often a good idea to be careful in your choice of default manager, in order to avoid a situation where overriding of get_query_set() results in an inability to retrieve objects you'd like to work with.
I found this to be wrong. The admin interface will only list the objects that are in the queryset that the default manager returns
This will only return objects with a status of finished=True in the admin, it's not even possible to access them directly by URL (default is to create objects that won't show)
Note: code written from scratch but you should get the idea, mind the order of appearance of the managers...
from django.db import models from django.contrib import admin class FinishedManager(models.Manager): def get_query_set(self): return super(FinishedManager, self).get_query_set().filter(finished=True) class Todo(models.Model): title = models.CharField(max_length=250) finished = models.BooleanField(default=False) finished_objects = FinishedManager() objects = models.Manager() class TodoAdmin(admin.ModelAdmin): model = Todo admin.site.register(Todo, TodoAdmin)
while having a Todo-class like below will return all objects:
class Todo(models.Model): title = models.CharField(max_length=250) finished = models.BooleanField(default=False) objects = models.Manager() finished_objects = FinishedManager()