[Django]-Model Manager in Django – No reference to model class?

5👍

When you create a model class in django, it calls add_to_class for each attribute on the model.

https://github.com/django/django/blob/1.6.5/django/db/models/base.py#L143

if what you’re trying to add the class has a contribute_to_class method, then it gets called instead of calling setattr

https://github.com/django/django/blob/1.6.5/django/db/models/base.py#L264

So when you assign the manager to the model class with

dahl_object = DahlBookManager()

contribute_to_class() is called on the manager class, which receives the model class. It saves this on self.model:

https://github.com/django/django/blob/1.6/django/db/models/manager.py#L69

get_queryset() then uses this reference to self.model:

https://github.com/django/django/blob/1.6/django/db/models/manager.py#L123

Leave a comment