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:
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”
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:
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.
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.
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!