我继承了为 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关闭autocommittransaction.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/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht