循环浏览一堆文件夹式内容,这些内容本身可能就是文件夹式的?
假设我有一个内容类型“文件夹”,其中包含 4 个项目。
+ MyObject
- Child1
- Child2
- Child3
+ Child4
- Child5
- Child6
假设我有另一种内容类型(我们称之为 Alias
)。这个Alias
主要是对另一个对象的引用,但是是文件夹式的:它可以包含一堆其他别名。我将在下面的树表示中使用 -->
来指示此引用。(“Reference”主要是一个名为“reference”的属性,它从目标对象接收 UID)。
假设 MyAlias
现在引用我的 MyObject
。
+ MyAlias --> MyObject
- (Nothing)
引用 MyObject
时,MyAlias
不知道 MyObject
是一个文件夹,因此内部 MyAlias 子级不存在。我需要循环遍历每个人,并在 MyAlias
中手动创建一个别名,这是对 MyObject
子项的引用(具有相同的结构)。一棵小树显示应该发生什么:
+ MyAlias --> MyObject
- Alias --> Child1
- Alias --> Child2
- Alias --> Child3
+ Alias --> Child4
- Alias --> Child5
- Alias --> Child6
我想知道迭代 MyObject
项的最佳方法,并使用某种循环并使用 invokeFactory< 与其他对象创建相同的结构/code> 在订阅者中。最后,我将同时存在两棵树:一棵是实际的文件夹和子项,另一棵是对同一文件夹和子项的引用。
(总结:类似于collective.alias,但以一种非常原始的形式,只是文件夹文档,因为我不能使用collective.alias。)
Suppose I have a content type, Folder, with 4 items.
+ MyObject
- Child1
- Child2
- Child3
+ Child4
- Child5
- Child6
Suppose I have another content type (let's call it Alias
). This Alias
is mainly a reference to another object but folderish: it can contain a bunch of other aliases. I'm going to use -->
to indicate this reference in the following tree representations.("Reference" is mainly an attribute called "reference" that receives the UID from the target object).
Suppose MyAlias
now references my MyObject
.
+ MyAlias --> MyObject
- (Nothing)
When referencing to MyObject
, MyAlias
doesn't know that MyObject
is a Folder, so the internal MyAlias children don't exist. I need to loop through everyone, and create, manually, an Alias inside MyAlias
, that is a reference (having the same structure) to MyObject
children. A little tree showing what should happen:
+ MyAlias --> MyObject
- Alias --> Child1
- Alias --> Child2
- Alias --> Child3
+ Alias --> Child4
- Alias --> Child5
- Alias --> Child6
I would like to know the best way to iterate through MyObject
items, and create the same structure with another objects, using some kind of loop and using invokeFactory
in a subscriber. In the end, I would have BOTH trees existing: one of the actual Folder and children, and another of references to this same Folder and children.
(Summarizing: something like collective.alias, but in a really primitive form, just folders an documents, since I can't use collective.alias.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最优雅和 Pythonic 的解决方案是编写一个递归生成器。假设这是一个方法:
那么这样
,您实际上不必将操作包装到函数/可调用中,并且没有控制反转。
The most elegant and Pythonic solution is to write a recursive generator. Assuming this is a method:
Then
This way, you don't actually have to wrap your action into a function/callable and there's no inversion of control.
递归可能有帮助
Recursivity may help
为什么不使用目录?该目录的存在是为了快速、安全地为您解决这些问题,以便您可以专注于重要的事情。因此,要查找某个路径中的所有对象:
您当然可以在查询中过滤更多内容。然后,
目录是您的朋友,阅读如何使用它。
Why would you not use the catalog? The catalog exists for solving these problems fast and securely for you, so that you can concentrate on things that matter. So, to find all the objects in some path:
You can of course filter more in your query. Then,
The catalog is your friend, read how to use it.
听起来您可能正在尝试进行内容类型迁移。如果是这种情况,请查看 Products.contentmigration。
It sounds like you may be trying to do a content type migration. If that's the case, take a look at Products.contentmigration.