【发布时间】:2012-02-02 08:40:19
【问题描述】:
以下代码在 Django 信号中,当副本数为“2”时,以下代码会创建 80+ 个副本,然后崩溃……怎么了?
def internal_signal(sender, instance, signal, created, *args, **kwargs):
for i in range(instance.number_of_copies):
item_copy = deepcopy(instance)
item_copy.id = item_copy.id + 1
item_copy.internal_barcode = "%s"%(item_copy.item_location.location_code)
item_copy.save()
post_save.connect(internal_signal, sender=Inventory)
谢谢。
编辑:呃!发现问题了,上面的代码是 Django 信号的 post_save 操作的一部分,所以每次 'save' 都会触发另一个循环,然后堆崩溃。
以编程方式创建“n”个对象并将其保存在 Django 中的最佳方法是什么?
【问题讨论】:
标签: python django deep-copy django-signals