title: Understanding managers and self in Django
body: <p>I would like to know how do managers work in Django. Let's use the following example (from The Definitive Guide to Django) as the model:</p>
  
  <pre><code>class BookManager(models.Manager):
    def title_count(self, keyword):
      return self.filter(title__icontains=keyword).count()
  
  class Book(models.Model):
    title = models.CharField()
    ...
    objects = BookManager()
  </code></pre>
  
  <p>Given that model, we can use <code>Book.objects.title_count('django')</code>. However, I'm not sure about what's going on in the background. When we use <code>title_count</code>, <code>self</code> refers to the instance <code>BookManager</code>, which knows about a method called filter because it's inheriting such a method from the <code>models.Manager</code> class, but how does it know that it should use filter in instances produced by the <code>Book</code> class?. </p>
  
  <p>I would naively think that title_count needs to do  <code>Books.objects.filter(title__icontains=keyword).count()</code> internally but it's using explicitly something like <code>objects.filter(title__icontains=keyword).count()</code> without referring to a particular class.</p>