W
W
wawa2018-03-17 21:43:58
Django
wawa, 2018-03-17 21:43:58

ModelManager methods VS Model classmethods - when to use?

As part of the fat models idea, if you need to add functionality to model objects, then I write a method for the model that works with a specific instance (a regular method with self).
In what cases to shove the code that works with several instances into the custom model manager, and in what cases - into the class method of the model?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Burnaev, 2018-03-18
@DmitryBurn

It depends on what problem you are solving. If you are somehow using/processing a recordset from a querieset, then it is better to use the custom querieset method. This will make it easy to apply the method by imposing various conditions on the querieset. If you do NOT mean the processing of the incoming queriset, but simply describe some general logic for the model class (and not for a specific instance), then it is better to use the class method of the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question