- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我继承了为 Django 1.4 编写的应用程序的一些代码。我们需要更新代码库以使用 Django 1.7,并最终将 1.8 作为下一个长期支持版本。
在一些地方它使用旧样式 @transaction.commit_manually
和 with 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 模型中。
我阅读这段代码的方式是
commit_manually
关闭autocommit
transaction.commit()
如果没有错误,将其存储到数据库中。model
是否是一个 unicode 实例,然后对其进行编码(我不确定这到底做了什么)model
实例化一个 XMLManipulator 对象model
(tostring
是一种保留样式表声明的 XMLManipulator 方法。)xml
对象在 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/
我几乎不敢在这里问这个问题,因为它似乎应该很容易通过谷歌或 ravendb.net 获得。但是,我一直很难找到将我的 RavenDB 升级到新版本的正确方法。我目前正在运行 573 版并希望升级到 6
这周我需要升级当前版本的 DNN。我目前使用的是 2.1.1。我不想每件事都做两次,所以,我有几个问题。 是否有升级工具或某些脚本可以帮助我进行升级。 我最好安装 4.9 还是 5.0。这是生产。 如
将 Sugarcrm 从 6.2 升级到 6.3 版本时遇到问题。当我升级本地 Sugarcrm 安装时,它可以工作,但是当我开始升级我的 Sugarcrm 6.2 站点并上传升级包时,它不会上传。
有没有办法绕过 Meteor 的自动更新功能?我坚持 Downloading meteor-tool@1.3.0_3... \ 当我尝试运行现有项目,或创建一个新项目或只是运行“
我已将应用内集成到我的 Andorid 应用程序中,用于单个产品 productone。 为此,我在我的 Google Play 控制台中创建了不同的产品 ID,如下所示: 1。 productone
我在将 TeamCity 版本 2017.1.1 升级到 2017.1.2 时遇到问题。这个问题涉及 TeamCity 和 PostgreSQL 的工作。我的工作: 停止 teamcity 进程 /e
我寻找了这个问题的具体答案,但找不到——即使是在 WAMPSERVER 网站上也是如此。我确定我忽略了它。 我有 Wampserver 2.0、MySQL 5.0.51b、PHP 5.2.6 和 Ap
我使用 Ubuntu 软件中心默认的 Eclipse 3.7。 我想将 Eclipse 升级到 kepler 版本,所以我添加了 repository 我收到以下错误消息: Cannot comple
你好 我只想安装 mercurial,但对于它需要 python 2.6 的所有版本,我尝试使用 .rpm 文件,但我唯一得到的是很多充满错误的行,它告诉我:需要安装在 2.6 之前和 2.5 之后的
我完全知道 Gradle 网站上有一些页面说明了如何升级,但仅限于 4.x 及更高版本。 我正在尝试关注 tutorial制作一个简单的“我的第一个”Minecraft 模组。在其中,您被告知安装 f
我们想升级 Kerberos(服务器和客户端) 当前:1.6.3-133.27.1 目标:1.6.3-133.49.97.1 问题是如果我们用包管理器升级它,下面会发生什么? KDC 数据库 所有主要
背景 原计划 2019 年发布的 Vue3,又经过一年的再次打磨,终于于去年 9 月正式发布。随后,不少 UI 组件库都积极参与适配,去年 12 月,Element-plus(
我有一个版本为 2.3.4 的 grails 项目,我需要尽可能升级到最新版本。查看文档我意识到从 2.x 到 3.x 有巨大的变化。 问题是:从 2 到 3、从 3 到 4、从 4 到 5 逐步升级
我正在将 API 项目从 .net5 升级到 .net6 它以前工作,现在它崩溃 内部异常消息“抛出了‘Unity.Exceptions.InvalidRegistrationException’类型
我将我的项目从 expo 44 升级到 expo 45,现在我有无数这样的错误: The module 'MaterialIcons' can't be used as JSX component.
我已经升级了掌 Helm 模板(手动) 以前的片段depoloyment.yaml : apiVersion: apps/v1beta2 kind: Deployment metadata: na
我正在尝试将我的 Scala Play Framework 应用程序升级到 2.8,这涉及将 SBT 升级到 1.x。 在我的 build.propeties 我有 sbt.version=1.3.5
我想在我的 Windows 服务器上安装 PHPUnit 3.7。我遵循了各种说明 here并以 PHPUnit 3.4.1 结束。当我尝试使用以下方法再次安装它时: pear update chan
Microsoft.Net 4.5 即将推出,我想在 MS 发布最终版本时升级我的 clickonce 应用程序。 我的问题是:已经安装了 clickonce 应用程序(使用 .net 4.0)的用户
为了将 Angular 8 更新到 9,我正在按照官方文档升级。 这建议首先更新到最新版本的 angular 8,例如: ng update @angular/core@8 @angular/cli@
我是一名优秀的程序员,十分优秀!