如何在循环djang中回滚事务

2024-05-20 17:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

如果循环中有异常抛出,我尝试回滚一组事务。但我不想在没有捕捉到异常的情况下跳出循环或抛出异常。在

我不希望在循环中的任何子进程抛出异常时保存业务逻辑。所以这意味着我不能将事务放入循环中,因为如果其中任何一个失败,它只会回滚特定子级的事务。在

parent = Parent.objects.get(pk='something')
exceptions = []
with transaction.atomic():
    for child in parent.children.all():
        try:
            # business logic which also saves other models
            # I don't want this saved if there is an exception for any object in the loop
        except Exception as e:
            exceptions.append({
                'id': child.id,
                'error': str(e),
            })
if len(exceptions) > 0:
    transaction.set_rollback(True)
    for exception in exceptions:
        Child.objects.filter(pk=exception['id']) \
            .update(error=exception['error']
    # more business logic and raise exception
    parent.is_blocked = True
    parent.save()
    # I don't want this exception to rollback all transactions
    raise Exception('Parent {} is blocked'.format(parent.id))

上面的代码有一个错误。这个信息相当直截了当。我正在尝试回滚块外的事务。在

django.db.transaction.TransactionManagementError: The rollback flag doesn't work outside of an 'atomic' block.

有人找到处理这种事情的方法吗。我希望我错过了一些简单的事情。如果你需要更多信息,请告诉我。在


Tags: inidforobjectsisexceptionerror事务
2条回答

您可以尝试一个生成器函数:

def function():
    for child in parent.children.all():
        try:
            yield result
        except Exception as e:
            yield exception

为了清楚起见,您可以查看: How to handle error thrown in a generator function

^{}

遵循文档,对于您的特殊情况,您的代码应该如下所示:

parent = Parent.objects.get(pk='something')
exceptions = []
try:
    with transaction.atomic():
        for child in parent.children.all():
            try:
                # business logic which also saves other models
                # I don't want this saved if there is an exception for any object in the loop
            except Exception as e:
                exceptions.append({
                    'id': child.id,
                    'error': str(e),
                })
        # raise exception handly to trigger rollback
        if len(exceptions) > 0:
            raise("raise for rollback")
except Exception as e:
    pass

if len(exceptions) > 0:
    for exception in exceptions:
        Child.objects.filter(pk=exception['id']) \
            .update(error=exception['error']
    # more business logic and raise exception
    parent.is_blocked = True
    parent.save()
    # I don't want this exception to rollback all transactions
    raise Exception('Parent {} is blocked'.format(parent.id))

相关问题 更多 >