gpt4 book ai didi

python - 将 transaction.commit_manually() 升级到 Django > 1.6

转载 作者:太空狗 更新时间:2023-10-29 21:03:10 25 4
gpt4 key购买 nike

我继承了为 Django 1.4 编写的应用程序的一些代码。我们需要更新代码库以使用 Django 1.7,并最终将 1.8 作为下一个长期支持版本。

在一些地方它使用旧样式 @transaction.commit_manuallywith transaction.commit_manually:

我对事务的一般了解还不够,但我想了解它们的用途,所以我可以删除它们(如果不需要)或将它们升级到较新的 set_autocommit(False) 或等价物。

我了解到 Django < 1.5 中的事务管理并不理想,而且对于大多数用例而言过于复杂。

数据库连接看起来是这样的,没有特殊的事务管理。 (使用 Postgres 9.3)

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}

没有特殊的交易中间件。

我发现以下观点特别令人费解。 (已编辑)

@UserRequiredDecorator
class XMLModelView(View):

@transaction.commit_manually
def get(self, request, *args, **kwargs):
user = request.user

xml_models = models.XMLModel.objects.filter(user=user).order_by('-created').all()
if xml_models:
xml_model = xml_models[0]
model = xml_model.xml_field
else:
obj = initialize_xml_model(user)
model = obj.xml_field

transaction.commit()
if isinstance(model, unicode):
model = model.encode('utf-8')

with transaction.commit_manually():
xml = XMLManipulator(model, remove_blank_text=True)
xml.insert_user_info(user)
xml.whitespace_cleanup()
model = xml.tostring()
del xml
transaction.commit()
return HttpResponse(model, content_type='text/xml')

其中 initialize_xml_model 是一个函数,它接受一个平面 xml 文件(xml 模型模板)并创建一个新的 XMLModel 对象。并且 insert_user_info 将存储在用户对象中的信息插入到 xml 模型中。

我阅读这段代码的方式是

  1. 我们使用commit_manually关闭autocommit
  2. >
    • 我们要么为用户获取最新的 XMLModel 对象,要么
    • 初始化一个新的 XMLModel 对象
  3. transaction.commit() 如果没有错误,将其存储到数据库中。
  4. 我们检查我们的 model 是否是一个 unicode 实例,然后对其进行编码(我不确定这到底做了什么)
  5. 我们开启一个新交易
  6. 使用 model 实例化一个 XMLManipulator 对象
  7. 插入内容并清理 xml
  8. 将 xml 实例作为字符串分配回 model(tostring 是一种保留样式表声明的 XMLManipulator 方法。)
  9. 删除xml对象
  10. 提交交易

在 5. 之后,唯一处理数据库(在读取中)的是 insert_user_info 方法。

我真的不明白为什么在特殊交易中会发生这种情况。没有写入数据库?

这个 View 没有其他方法,只有get。

我可能在这里遗漏了一些重要的东西,请随时提出任何问题或了解更多信息。

这里真的需要事务吗?如果需要,如何重写以适应新的 transaction.set_autocommit

如有任何帮助或指点,我们将不胜感激。

最佳答案

现在执行此操作的一个好方法是使用 transaction.atomic。在你的例子中我会这样做:

from django.db import transaction

@UserRequiredDecorator
class XMLModelView(View):

def get(self, request, *args, **kwargs):

with transaction.atomic():
user = request.user

xml_models = models.XMLModel.objects.filter(user=user).order_by('-created').all()
if xml_models:
xml_model = xml_models[0]
model = xml_model.xml_field
else:
obj = initialize_xml_model(user)
model = obj.xml_field

if isinstance(model, unicode):
model = model.encode('utf-8')

with transaction.atomic():
xml = XMLManipulator(model, remove_blank_text=True)
xml.insert_user_info(user)
xml.whitespace_cleanup()
model = xml.tostring()
del xml

return HttpResponse(model, content_type='text/xml')

关于python - 将 transaction.commit_manually() 升级到 Django > 1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29149714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com