Django - 在视图结束后进行交易

时间:2018-01-08 16:26:28

标签: python django transactions

我正在使用Django 1.10 - 调用视图后使用中间件并进行一些数据库更改(见下文)。问题是 - 假设在请求(调用View)上发生了一些数据库更改 - 在中间件中访问该部分时,我是否仍处于同一事务中,或者它已经完成了吗?

def __init__(self, get_response):
    self.get_response = get_response

def __call__(self, request):
    # Code to be executed for each request before
    # the view (and later middleware) are called.
    MyControlMiddleware.init(True)

    response = self.get_response(request)
    # Code to be executed for each request/response after
    # the view is called.
    if response:
        # does the transaction still live here?
        # ..... code to run after request is done....
        MyControlMiddleware.clear()

0 个答案:

没有答案