I have read the definition in the official Django documentation, and I am still confused by what a Manager does.
The documentation says that they allow you to operate on database tables/models, but I still don’t understand this.
Can someone explain managers and their role to me? An answer with an example would be preferable.
A manager is usually something hidden away from django programmers that django uses to interface between
modelcode and the database backend.When you query the django ORM, you do so through calls to
In this case, the
objectspart of the function is what is returned by the manager. If you wanted MyModel to only ever getblueMyModelinstances (the database might containredmodels too) then you could create a manager and hack your model thusand calling
would only return objects with
colourasblue. Note, this is a very poor way to filter models!One would usually need to modify a
Managerinterface if they were going to modify theQuerySets that a manager would usually return or if you needed to add “table” level queries (rather than regular django “row” level). The documentation for managers is quite complete and contains several examples.