我在Django 1.8安裝上收到錯誤This is forbidden when an 'atomic' block is active.
。當「原子」塊處於活動狀態時,這是禁止的。 Django 1.8
我試圖在這兩個與transaction.atomic():
包裹它,以及創建IntegrityError
這裏的例外是我的代碼:
item = Item.objects.filter(item_id = data['item_id'])[:1][0]
iserializer = ItemSerializer(item, data=data, partial=True)
try:
with transaction.atomic():
if iserializer and iserializer.is_valid():
iserializer.save()
except IntegrityError:
pass
可能是什麼造成的呢?爲什麼我的異常處理不起作用以防止接收到這個錯誤?
這裏的回溯:
Traceback (most recent call last):
File "<console>", line 4, in <module>
File "/home/vagrant/client/venv/bin/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 186, in save
self.instance = self.update(self.instance, validated_data)
File "/home/vagrant/client/venv/bin/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 880, in update
instance.save()
File "/vagrant/client/client/items/models.py", line 45, in save
transaction.commit()
File "/home/vagrant/client/venv/bin/local/lib/python2.7/site-packages/django/db/transaction.py", line 42, in commit
get_connection(using).commit()
File "/home/vagrant/client/venv/bin/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 172, in commit
self.validate_no_atomic_block()
File "/home/vagrant/client/venv/bin/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 322, in validate_no_atomic_block
"This is forbidden when an 'atomic' block is active.")
TransactionManagementError: This is forbidden when an 'atomic' block is active.
請修復您的代碼縮進 - 它不是有效的Python原樣 - 併發布更多詳細信息 - 特別是完整的原始回溯。 –
我認爲它是一個'TransactionManagementError'。它是在「保存」行中提出的嗎?這不就是說你不能在原子交易中做到這一點嗎?堆棧跟蹤將會有所幫助。 – tdelaney
這是一個TransactionManagementError。我會發布回溯。 而我的代碼縮進關閉的道歉 - 我編輯文字處理器中的文本,以保持客戶端的詳細信息匿名。 –