我正在研究Django 1.8项目,该项目已在管理部分中设置了到达功能。 ModelAdmin
子类之一重写response_change
方法来处理APPROVE
按钮的操作。因此,代码如下所示:
class MyModelAdmin(ModelAdmin):
def response_change(self, request, obj):
rel = obj.rel
rel.title = obj.title
rel.save()
serialize('json', (rel, ))
obj.approved = true
obj.save()
return super().response_change(request, obj)
在序列化行上出现错误:
File "/env/lib/python3.6/site-packages/django/core/serializers/__init__.py", line 129, in serialize
s.serialize(queryset, **options)
File "/env/lib/python3.6/site-packages/django/core/serializers/base.py", line 68, in serialize
self.handle_m2m_field(obj, field)
File "/env/lib/python3.6/site-packages/django/core/serializers/python.py", line 77, in handle_m2m_field
for related in getattr(obj, field.name).iterator()]
File "/env/lib/python3.6/site-packages/django/core/serializers/python.py", line 76, in <listcomp>
self._current[field.name] = [m2m_value(related)
File "/env/lib/python3.6/site-packages/django/db/models/query.py", line 238, in iterator
results = compiler.execute_sql()
File "/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
cursor.execute(sql, params)
File "/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 59, in execute
self.db.validate_no_broken_transaction()
File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 327, in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
当前事务中发生错误。在“ atomic”块结束之前,您无法执行查询。
如果我删除序列化行-相同的错误出现在obj.save()
行上。最奇怪的是,这种错误是在某些对象上引发的,而不是在其他对象上引发的。
是什么原因造成的?如何解决?