Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django Managers vs. staticmethod on Model class directly

Tags:

python

django

After reading up on Django Managers, I'm still unsure how much benefit I will get by using it. It seems that the best use is to add custom queries (read-only) methods like XYZ.objects.findBy*(). But I can easily do that with static methods off of the Model classes themselves.

I prefer the latter always because:

  1. code locality in terms of readability and easier maintenance
  2. slightly less verbose as I don't need the objects property in my calls
  3. Manager classes have weird rules regarding model inheritance, might as well stay clear of that.

Is there any good reason not to use static methods and instead use Manager classes?

like image 338
Xerion Avatar asked Oct 07 '11 20:10

Xerion


People also ask

What is the difference between Classmethod and Staticmethod?

Class method can access and modify the class state. Static Method cannot access or modify the class state. The class method takes the class as parameter to know about the state of that class. Static methods do not know about class state.

What is the point of @staticmethod?

@staticmethod variable is used with the functions where you do not need any reference to the class' object within the function i.e. you do not any usage of self for accessing any of class' property or function.

Why do we need model manager in Django?

A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application. The way Manager classes work is documented in Making queries; this document specifically touches on model options that customize Manager behavior.

What is the purpose of Classmethod and Staticmethod in Python?

The difference between the Class method and the static method is: A class method takes cls as the first parameter while a static method needs no specific parameters. A class method can access or modify the class state while a static method can't access or modify it.


1 Answers

Adding custom queries to managers is the Django convention. From the Django docs on custom managers:

Adding extra Manager methods is the preferred way to add "table-level" functionality to your models.

If it's your own private app, the convention word doesn't matter so much - indeed my company's internal codebase has a few classmethods that perhaps belong in a custom manager.

However, if you're writing an app that you're going to share with other Django users, then they'll expect to see findBy on a custom manager.

I don't think the inheritance issues you mention are too bad. If you read the custom managers and model inheritance docs, I don't think you'll get caught out. The verbosity of writing .objects is bearable, just as it is when we do queries using XYZ.objects.get() and XYZ.objects.all()

Here's a few advantages of using manager methods in my opinion:

  1. Consistency of API. Your method findBy belongs with get, filter, aggregate and the rest. Want to know what lookups you can do on the XYZ.objects manager? It's simple when you can introspect with dir(XYZ.objects).

  2. Static methods "clutter" the instance namespace. XYZ.findBy() is fine but if you define a static method, you can also do xyz.findBy(). Running the findBy lookup on a particular instance doesn't really make sense.

  3. DRYness. Sometimes you can use the same manager on more than one model.

Having said all that, it's up to you. I'm not aware of a killer reason why you should not use a static method. You're an adult, it's your code, and if you don't want to write findBy as a manager method, the sky isn't going to fall in ;)

For further reading, I recommend the blog post Managers versus class methods by James Bennett, the Django release manager.

like image 167
Alasdair Avatar answered Sep 28 '22 06:09

Alasdair