Renaming my.packages to my.package

喜欢而已 提交于 2019-12-10 14:21:30

问题


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?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/9570902/renaming-my-packages-to-my-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!