如何在portal_factory 中创建并填充Plone 对象?
我需要从第二个对象(object2
- 它不是新对象的父对象)的 View 创建一个(原型)对象。它需要预先填充来自 Request 和 object2
的数据。
简单的解决方案似乎是在架构字段上使用 "default_method" ,这可以适用于请求中的数据,但我不相信我可以从那里访问视图,因此也不适用于object2
。无论如何,其中一个字段是 object2
的 ReferenceField,并且我读到 ReferenceField 忽略“default_method”。
另一个选项是在portal_factory 中创建它,设置其默认值,然后显示“添加”页面,允许用户根据需要修改内容,或者退出而不实际创建对象。完美的,除了,有多种可用于创建对象的方法,(invokeFactory()
、_createObjectByType()
、_constructInstance()
& ; createObject()
,据我所知),只有 createObject
实际上将对象保留在 Portal_factory 中 - 因为它只返回一个字符串(对象的“添加”页面的 URL) ),不会接受关键字参数,并且似乎不会通知任何事件(当然不是 IObjectCreatedEvent),在将用户引导到编辑页面之前,我不知道如何使用我的数据对其进行修改。
I need to create an (archetypes) object from a View of a second object (object2
- which is not the parent of the new object). It needs to be prepopulated with data from the Request, and from object2
.
The simple solution would seem to be to use "default_method" on the schema fields, and that can work for the data from the Request, but I don't believe I have access to the View from there, and therefore not to object2
, either. In any case, one of the fields is a ReferenceField to object2
and I've read that ReferenceField ignores "default_method".
The other option is to create it inside the portal_factory, set its defaults, and then display the Add page, allowing the user to modify the content as required, or to exit without actually creating the object. Perfect, except that, of the multitude of methods available to create an object, (invokeFactory()
, _createObjectByType()
, _constructInstance()
& createObject()
, that I know of), only createObject
actually leaves the object in the portal_factory - and since it only returns a string (the URL of the object's Add page), won't accept keyword arguments, and doesn't seem to notify any events (certainly not IObjectCreatedEvent), I can't see how to modify that with my data before directing the user to the edit page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我在无法使用 createObject 时推荐的模式:
This is the pattern that I recommend when is not possible to use createObject:
哦。终于想通了。
createObject
从任何真正意义上来说,并没有创建一个对象。它实际上只是创建表单的 URL。调用
.createObject()
,获取表单的 URL,附加您想要的值作为查询参数:就可以了。
Doh. Finally figured it out.
createObject
doesn't, in any real sense, create an object. It really is just a URL to the creation form.Call
.createObject()
, get the URL for the form, attach the values you want as query parameters:will do it.