首页 > 解决方案 > Django 如何在 transaction.atomic() 中保存日志

问题描述

假设您在确认付款后正在处理订单。

with transaction.atomic(): 

   do something like subtract stock
   log stuff in DB so that we can later investigate what went wrong 
   if something goes wrong: # this line of code may reside in a function nested deep 
     raise
   do more to finalize the order

当出现异常时,db 可以正常回滚,但我希望将日志记录保留在 db 中。

我正在考虑创建一个芹菜任务并记录背景,(不确定它是否会起作用)。

有更好的选择吗?

标签: django

解决方案


我可以理解您的问题,这可能会对您有所帮助:-

try:
   with transaction.atomic():
      do something like subtract stock
except:
   log staff into db to investigate what went wrong 
do more to finalize the order

推荐阅读