python - How can i use signals in django bulk create -
i have code
task.objects.bulk_create(ces)
now signal
@receiver(pre_save, sender=task) def save_hours(sender, instance, *args, **kwargs): logger.debug('test')
now signal not triggered in bulk create
i using django 1.8
as mentioned bulk_create
not trigger these signals -
https://docs.djangoproject.com/en/1.8/ref/models/querysets/#bulk-create
this method inserts provided list of objects database in efficient manner (generally 1 query, no matter how many objects there are).
this has number of caveats though:
- the model’s save() method not called, , pre_save , post_save signals not sent.
- it not work child models in multi-table inheritance scenario.
- if model’s primary key autofield not retrieve , set primary key attribute, save() does.
- it not work many-to-many relationships.
- the batch_size parameter controls how many objects created in single query. default create objects in 1 batch, except sqlite default such @ 999 variables per query used.
so have trigger them manually. if want models can override bulk_create
, send them -
class custommanager(models.manager): def bulk_create(items,....): super().bulk_create(...) in items: [......] # code send signal
then use manager -
class task(models.model): objects = custommanager() ....
Comments
Post a Comment