Reputation: 31653
I have a custom manager added to model like that:
class StandardManagerModel(models.Model):
pass
class PublishableManager(models.Manager):
pass
class Publishable(models.Model):
published_objects = PublishableManager()
This removes the default .objects
manager from the model. How can i retrieve a default manager declared in model class? I would like a function like get_default_manager()
:
manager = get_default_manager(Publishable)
assert manager is Publishable.published_objects
manager = get_default_manager(StandardManagerModel)
assert manager is StandardManagerModel.objects
Upvotes: 2
Views: 3355
Reputation: 487
Looks like you don't need a function at all. It's stored as an attribute on the model:
Model._default_manager
There's also Model._base_manager
, which I can only assume is what the default manager would be if you don't provide one.
Upvotes: 5
Reputation: 43300
From the docs
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 the first Manager defined in a class as the “default” Manager, and several parts of Django (including dumpdata) will use that Manager exclusively for that model. As a result, it’s a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_queryset() results in an inability to retrieve objects you’d like to work with.
You can supply the default manager as well
objects = models.Manager()
published_objects = PublishableManager()
Upvotes: 0