2015-07-21 61 views
8

我已經使用Django REST Framework從2.x版本更新到最後一個穩定版本(3.1.3)。在我的序列化程序中修復了一些不推薦的用法之後,我運行了python manage.py test以確保沒有任何內容被破壞。當Django REST Framework故意引發錯誤時發生事務錯誤

一切工作正常,除了我測試無效的REST請求(有意)的一些測試。例如:

def test_get_events_of_other_user(self): 
    # Assume the setUp log the user1 with self.client 
    # and here, there is some code initializing an 
    # event for user2 
    response = self.client.get("/event/123/") 
    self.assertEqual(404, response.status_code) 

的視圖集中生成一個ORM查詢像MyEventModel.objects.get(user=request.user, pk=123),這顯然提高了在DRF 404例外,因爲沒有對象應該在這裏返回(我知道,它應該是一個403是REST -compliant ...)。但是,這引起了TransactionManagementError

Traceback (most recent call last): 
    [...] 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 85, in view 
    return self.dispatch(request, *args, **kwargs) 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/rest_framework/views.py", line 456, in dispatch 
    response = self.handle_exception(exc) 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/rest_framework/views.py", line 421, in handle_exception 
    response = exception_handler(exc, context) 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/rest_framework/views.py", line 81, in exception_handler 
    set_rollback() 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/rest_framework/compat.py", line 277, in set_rollback 
    transaction.set_rollback(True) 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/django/db/transaction.py", line 215, in set_rollback 
    return get_connection(using).set_rollback(rollback) 
    File "/my_virtual_env_path/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 372, in set_rollback 
    "The rollback flag doesn't work outside of an 'atomic' block.") 
TransactionManagementError: The rollback flag doesn't work outside of an 'atomic' block. 

它看起來像我的中間件之一是做視後,一些SQL查詢,並擊中了transaction closed by DRF, since the 3.1.3。期望的行爲(即得到一個404 NOT FOUND)與3.1.2 ...

一起工作有沒有人有我如何可以避免回滾的想法? DRF是否是一種誤解?

回答

2

這看起來像DRF中已經修復的錯誤。正如你可以看到here他們已經改變了代碼,以便它只在原子塊中回滾。

由於沒有更新的版本發佈,您可以將相同的修補程序應用到您的已安裝的代碼,或使用較早的版本,直到3.1.3被取代。

+0

確實,它適用於此修復程序。我將繼續使用3.1.2版本,直到3.1.4版本結束。感謝您的鏈接! –