Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use signals in django bulk create

I have this code

Task.objects.bulk_create(ces)

Now this is my signal

@receiver(pre_save, sender=Task)
def save_hours(sender, instance, *args, **kwargs):
    logger.debug('test')

Now this signal is not triggered in bulk create

I am using django 1.8

like image 281
user3214546 Avatar asked Jun 03 '15 23:06

user3214546


People also ask

How do you use signals in Django?

There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown.

What is the use of the Post_delete signal in Django?

Django Signals - post_delete() To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.

Where is Pre_save signal in Django?

pre_save. This is sent at the beginning of a model's save() method. Arguments sent with this signal: sender.

What is bulk create in Django?

Django bulk_create can help us optimize our application using a small number of database calls to save a lot of data. In other words, bulk_create can save multiple model instances into the database using only one database call.


2 Answers

As mentioned bulk_create does not trigger these signals -

https://docs.djangoproject.com/en/1.8/ref/models/querysets/#bulk-create

This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are).

This has a number of caveats though:

  • The model’s save() method will not be called, and the pre_save and post_save signals will not be sent.
  • It does not work with child models in a multi-table inheritance scenario.
  • If the model’s primary key is an AutoField it does not retrieve and set the primary key attribute, as save() does.
  • It does not work with many-to-many relationships.
  • The batch_size parameter controls how many objects are created in single query. The default is to create all objects in one batch, except for SQLite where the default is such that at most 999 variables per query are used.

So you have to trigger them manually. If you want this for all models you can override the bulk_create and send them yourself like this -

class CustomManager(models.Manager):
    def bulk_create(items,....):
         super().bulk_create(...)
         for i in items:
              [......] # code to send signal

Then use this manager -

class Task(models.Model):
    objects = CustomManager()
    ....
like image 114
brainless coder Avatar answered Sep 30 '22 18:09

brainless coder


Iterating on the answer above:

Python 2:

class CustomManager(models.Manager):
    def bulk_create(self, objs, **kwargs):
        #Your code here
        return super(models.Manager,self).bulk_create(objs,**kwargs)  

Python 3:

class CustomManager(models.Manager):
    def bulk_create(self, objs, **kwargs):
        #Your code here
        return super(CustomManager, self).bulk_create(objs,**kwargs)  

class Task(models.Model):
    objects = CustomManager()
    ....

Complete answer in python 2:

class CustomManager(models.Manager):

def bulk_create(self, objs, **kwargs):
    a = super(models.Manager,self).bulk_create(objs,**kwargs)
    for i in objs:
        post_save.send(i.__class__, instance=i, created=True)
    return a
like image 30
Felipe Sens Avatar answered Oct 02 '22 18:10

Felipe Sens