我如何在django批量创建中使用信号

时间:2021-05-02 20:49:44

I have this code

我有这个代码

Task.objects.bulk_create(ces)

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

我正在使用django 1.8

1 个解决方案

#1


12  

As mentioned bulk_create does not trigger these signals -

如上所述,bulk_create不会触发这些信号 -

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

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).

此方法以有效的方式将提供的对象列表插入到数据库中(通常只有1个查询,无论有多少个对象)。

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.
  • 不会调用模型的save()方法,也不会发送pre_save和post_save信号。
  • 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.
  • 如果模型的主键是AutoField,则它不会像save()那样检索和设置主键属性。
  • 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.
  • batch_size参数控制在单个查询中创建的对象数。默认设置是在一个批处理中创建所有对象,但SQLite除外,其中默认值为每个查询最多使用999个变量。

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 -

所以你必须手动触发它们。如果你想要所有型号的这个,你可以覆盖bulk_create并像这样自己发送 -

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()
    ....

#1


12  

As mentioned bulk_create does not trigger these signals -

如上所述,bulk_create不会触发这些信号 -

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

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).

此方法以有效的方式将提供的对象列表插入到数据库中(通常只有1个查询,无论有多少个对象)。

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.
  • 不会调用模型的save()方法,也不会发送pre_save和post_save信号。
  • 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.
  • 如果模型的主键是AutoField,则它不会像save()那样检索和设置主键属性。
  • 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.
  • batch_size参数控制在单个查询中创建的对象数。默认设置是在一个批处理中创建所有对象,但SQLite除外,其中默认值为每个查询最多使用999个变量。

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 -

所以你必须手动触发它们。如果你想要所有型号的这个,你可以覆盖bulk_create并像这样自己发送 -

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()
    ....