Django Define custom managers


Example

Very often it happens to deal with models which have something like a published field. Such kind of fields are almost always used when retrieving objects, so that you will find yourself to write something like:

my_news = News.objects.filter(published=True)

too many times. You can use custom managers to deal with these situations, so that you can then write something like:

my_news = News.objects.published()

which is nicer and more easy to read by other developers too.

Create a file managers.py in your app directory, and define a new models.Manager class:

from django.db import models


class NewsManager(models.Manager):

    def published(self, **kwargs):
        # the method accepts **kwargs, so that it is possible to filter
        # published news
        # i.e: News.objects.published(insertion_date__gte=datetime.now)
        return self.filter(published=True, **kwargs)

use this class by redefining the objects property in the model class:

from django.db import models

# import the created manager
from .managers import NewsManager

class News(models.Model):
    """ News model
    """
    insertion_date = models.DateTimeField('insertion date', auto_now_add=True)
    title = models.CharField('title', max_length=255)
    # some other fields here
    published = models.BooleanField('published')

    # assign the manager class to the objects property
    objects = NewsManager()

Now you can get your published news simply this way:

my_news = News.objects.published()

and you can also perform more filtering:

my_news = News.objects.published(title__icontains='meow')