Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "Manager" in django?

Tags:

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.

like image 431
masterpiece Avatar asked Feb 04 '13 14:02

masterpiece


People also ask

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 is a model manager?

careertrend. Becoming a model manager means overseeing the development and business of a clothing or fashion model. It is different from an agent who arranges auditions. Becoming a model manager requires understanding the industry and the unique attributes of your clients.

How do I create a custom manager in Django?

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.

Why might you want to write a custom model manager?

Custom managers There are two reasons you might want to create a custom manager for a model. You might want to add custom methods to the manager so it can have extra functionality. You might want to modify the initial QuerySet that the manager returns.


1 Answers

A manager is usually something hidden away from django programmers that django uses to interface between model code and the database backend.

When you query the django ORM, you do so through calls to

from my_app.models import MyModel  mms = MyModel.objects.all() 

In this case, the objects part of the function is what is returned by the manager. If you wanted MyModel to only ever get blue MyModel instances (the database might contain red models too) then you could create a manager and hack your model thus

class BlueManager(models.Manager):     def get_query_set(self):         return super(BlueManager, self).get_query_set().filter(colour='Blue')  class MyModel(models.Model):      colour = models.CharField(max_length=64)      blue_objects = BlueManager() 

and calling

MyModel.blue_objects.all() 

would only return objects with colour as blue. Note, this is a very poor way to filter models!

One would usually need to modify a Manager interface if they were going to modify the QuerySets 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.

like image 73
danodonovan Avatar answered Sep 24 '22 05:09

danodonovan