Help! My Models feel Bloated! Why you should write a custom Manager

Posted by Chris Bartos on February 27, 2016

Have you heard this advice?

You should follow the “Fat models / Skinny views Philosophy”


I think the logic should be in the model

And then, you hear some other advice that confuses you, Right? Have you ever heard this?

Fat models aren’t necessarily a good thing…

Which advice is correct? How do you proceed now that you’re all confused and frustrated at all your bloated models?

You don’t want bloated models though…

You know architecture is important. You know that fat models are preferable to fat views. But, you’re confused because you also have a very messy and bloated model that is getting harder and harder to maintain!

That’s why you really shouldn’t have fat models OR fat views. Developers have a hard time following strict dogma because there are always exceptions to the rule.

So, how can YOU break up your Django models into separate and clean utility classes? How do you clean up your model bloat? Create a custom manager!

Why use a Custom Manager?

Let’s assume you have a model that looks like the following:

class Person(models.Model):
     email = models.EmailField(max_length=250)
     first_name = models.CharField(max_length=45)
     last_name = models.CharField(max_length=45)

You might be tempted to write a function that gets a Person by “email”, then you might write a similar function to retrieve a Person by “last_name”

Something like:

def get_person_by_email(email):
     return Person.objects.get(email=email)

def get_person_by_last_name(last_name):
     return Person.objects.get(last_name=last_name)

Then, over time, you decide that it would be awesome if you could get all the people in your system with similar domain names in their emails. I want a count of all the people who signed up for my services that have “gmail” email addresses. You could do something like this:

def count_people_by_email_domain(domain):
     return len(Person.objects.filter(email__endswith="domain"))

You could see that you might have an infinite number of small functions that can manipulate the data anyway that you see fit.

Why not put those functions inside a custom Manager class instead?

That way, instead of writing a bunch of tiny functions and having to import them everywhere you need them, all you have to do is import the model and run the function on the model’s custom Manager class instead!

Let me show you what I mean.

from models import Person

# using a custom Manager on email...
people = Person.emails.filter_domain(domain)
person = Person.emails.get(email)

# using a custom Manager on last_name...
person = Person.last_names.get(last_name)

# etc...

How to Implement a custom manager…

Have I sold you on Custom Managers yet? The great news is that you can break your models up into a bunch of small, specific Manager classes and use them on your models.

Here is a simple Manager class that you can try out right now on our Person Model.

from django.db import models

# maybe through your Manager classes into ""
class EmailManager(models.Manager):
     def filter_domain(self, domain):
          return super(EmailManager, self).get_queryset().filter(email__endswith=domain)

     def get(self, email):
          return super(EmailManager, self).get_queryset().get(email=email)

class Person(models.Model):
     # our model data...

     emails = EmailManager()

That’s how you create a manager that encapsulates most of the tiny functions laying around your model. Just throw them into a customer Manager, and you’ll be much happier and so will your models!

Similar Posts

What do you do when 'makemigrations' is telling you About your Lack of Default Value
How to Implement Many-To-Many Relationship in Django
Test your Queries before they make it into Production using the Django Shell
I have a ChoiceField Callable that Includes a Queryset but it Breaks my Django app
Simple Method of Updating Models with ModelForms
How to filter a DateTimeField by today's date in Django

Need help with Django REST Framework? Django REST Framework documentation a little confusing?

Join me for my FREE Django REST Framework email course:

Django REST Framework Email Course

You'll get 1 lesson everyday for 7 days.
You'll learn:

  • Serializers
  • Request Methods
  • Endpoints
  • Basic Authentication
  • JQuery Integration
  • AngularJS Integration
Powered by ConvertKit