The Django documentation contains the following note:
You may be tempted to customize the init method. It can be a question. Rather than overriding init , try using one of these approaches:
Add a classmethod on the model class:
from django.db import models class Book(models.Model): title = models.CharField(max_length=100) @classmethod def create(cls, title): book = cls(title=title) # do something with the book return book book = Book.create("Pride and Prejudice")Add a method to a custom manager (usually preferred):
class BookManager(models.Manager): def create_book(self, title): book = self.create(title=title) # do something with the book return book class Book(models.Model): title = models.CharField(max_length=100) objects = BookManager() book = Book.objects.create_book("Pride and Prejudice")
Perhaps a similar question has already been asked, but still, what is the difference between creating an instance of a model through @classmethod and through Manager ? I want to understand more deeply the difference between these two approaches, and why it is precisely the second one I am preferred.