How to override bulk_create method? I try this
class SomeModel(models.Model):
field = models.CharField()
def bulk_create(self, objs, batch_size=None):
#do something
return super(SomeModel, self).bulk_create(objs, batch_size)
But it doesn't work. When I run this code
SomeModel.objects.bulk_create(objects_list)
It's create new objects, but it doesn't use my override bulk_create method. Is it possible to override bulk_create? And how?
bulk_create
is a method on the Manager
class, and SomeModel.objects
is an instance of Manager
. You need to subclass Manager
and override the method there, then add the manager to SomeModel
:
class SomeModelManager(models.Manager):
def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
...
class SomeModel(models.Model):
objects = SomeModelManager()
See the documentation on custom managers for more information.