Reading the Django docs, it advices to make a custom creation method for a model named Foo
by defining it as create_foo
in the manager:
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")
My question is that why is the previous one preferred to simply overriding the base class's create
method:
class BookManager(models.Manager):
def create(self, title):
book = self.model(title=title)
# do something with the book
book.save()
return book
class Book(models.Model):
title = models.CharField(max_length=100)
objects = BookManager()
book = Book.objects.create("Pride and Prejudice")
Imo it seems that only overriding create
will prevent anyone from accidentally using it to make a illformed model instance, since create_foo
can always be bypassed completely:
class BookManager(models.Manager):
def create_book(self, title):
book = self.create(title=title, should_not_be_set_manually="critical text")
return book
class Book(models.Model):
title = models.CharField(max_length=100)
should_not_be_set_manually = models.CharField(max_length=100)
objects = BookManager()
# Can make an illformed Book!!
book = Book.objects.create(title="Some title", should_not_be_set_manually="bad value")
Is there any advantage in doing it like the docs suggest, or is actually overriding create
just objectively better?