From 674f62a05adc3c48202ac8b692a89e4e71856c30 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 12 Sep 2025 12:40:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20base=20=E4=BF=AE=E6=94=B9=5Fshould=5Fuse?= =?UTF-8?q?=5Ftransaction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/utils/viewsets.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/utils/viewsets.py b/apps/utils/viewsets.py index 1e2758d8..ad6de925 100755 --- a/apps/utils/viewsets.py +++ b/apps/utils/viewsets.py @@ -22,6 +22,10 @@ from django.db import connection from django.core.exceptions import ObjectDoesNotExist from django.db import transaction +def enable_transaction(func): + """装饰器 标记这个action需要事务""" + func._enable_transaction = True + return func class CustomGenericViewSet(MyLoggingMixin, GenericViewSet): """ @@ -72,18 +76,14 @@ class CustomGenericViewSet(MyLoggingMixin, GenericViewSet): def _should_use_transaction(self, request): """判断当前请求是否需要事务""" - # 标准的写操作需要事务 - if request.method in ('POST', 'PUT', 'PATCH', 'DELETE'): - # 但还要看具体是哪个action - action = self.action_map.get(request.method.lower(), {}).get(request.method.lower()) - if action in ['create', 'update', 'partial_update', 'destroy']: - return True - - # 自定义的action:可以通过在action方法上添加装饰器或特殊属性来判断 - action = getattr(self, self.action, None) if self.action else None - if action and getattr(action, 'requires_transaction', False): + # 1. 标准写操作需要事务 + if self.action in ['create', 'update', 'partial_update', 'destroy']: + return True + action_method = getattr(self, self.action, None) + if not action_method: + return False + elif hasattr(action_method, '_enable_transaction'): return True - return False def finalize_response(self, request, response, *args, **kwargs):