将my.packages重命名为my.package [英] Renaming my.packages to my.package
问题描述
my.packages是src目录中的自定义原型程序包. Plone实例中的数千个项目及其类型被添加.我想将包重命名为my.package.通过简单地卸载my.packages并安装my.package,我发现 http://localhost:8080/mysite /myfolder/my-item 显示<persistent broken my.packages.content.mytype.MyType instance '\x00\x00\x00\x00\x00Un^'>
.我应该做迁移吗?还是有解决此问题的简单方法?
my.packages is a custom archetypes package in the src directory. Thousands of items in the Plone instance are added with its types. I want to rename the package as my.package. By simply uninstalling my.packages and installing my.package, I find http://localhost:8080/mysite/myfolder/my-item showing <persistent broken my.packages.content.mytype.MyType instance '\x00\x00\x00\x00\x00Un^'>
. Should I have to do migration? Or is there a simple way to fix this issue?
推荐答案
通过使用sys.modules
进行混淆,可以创建一个向后兼容的别名.在您的软件包__init__.py
:
You can create an alias for backward compatibility, by fudzing with sys.modules
. Do this in your package __init__.py
:
import sys
sys.modules['my.packages'] = sys.modules[__name__]
通过这种方式,持久性机制仍可以找到您的类.
This way the persistence machinery can find your classes still.
发生的事情是,当您的原型实例保留在ZODB中时,持久性机制将类的模块路径(点划线的python路径,例如my.packages.types.foobar.FooBar
)存储在存储的数据中.从ZODB恢复对象时,该路径将用于重新创建实例.重命名程序包时,所有这些引用都被破坏了.
What happens is that when your Archetypes instances are persisted in the ZODB, the persistence machinery stores a module path for the class (a dotted python path such as my.packages.types.foobar.FooBar
) in the stored data. When restoring an object from the ZODB, that same path is then used to re-create your instances. When you rename your package, all these references are broken.
使用上述技巧,很好的是,如果在事务中更改了对象并将其再次写入ZODB,将存储 new 模块路径.因此,可以想象导致对该程序包中的所有Archetypes实例进行写操作,以使迁移永久进行,因此您可以再次删除上述变通方法.
With the above trick, the nice thing is that if your object were changed and written to the ZODB again in a transaction, the new module path will be stored. You could thus conceivable cause a write to all your Archetypes instances from this package to make the migration permanent so you can remove the above work-around again.
这篇关于将my.packages重命名为my.package的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!