将原型新闻项迁移到敏捷内容类型 [英] Migrating Archetype news items to Dexterity content types
问题描述
我正在尝试将新闻项目中的内容复制到我编写的其他内容类型中.在我的脚本中,我有news
项目和project
项目.第二个project
是使用敏捷定义的内容类型.如果能以另一种方式将图像和正文文本从news
复制到project
,那就太好了.
I'm trying to copy the content in news items to other content type that I wrote. In my script I have the news
item and the project
item. The second, project
, is a content type defined using Dexterity. It would be wonderful if I could to copy the image and the body text from news
to project
in the next way.
project.text = news.text
project.image = news.image
在project
模式中将文本和图像定义为RichText和NamedBlobImage的位置.我不知道新闻项中的属性.我只知道我可以使用方法getImage()
在新闻项中获取图像,但是将其分配给项目会在渲染项目时产生错误.
Where text and image are defined in the project
schema as RichText and NamedBlobImage. I don't know how the attributes are in the news item. I only know that I can get the image in the news item using the method getImage()
but assign it to the project generates an error when rendering the project.
所以我需要一些指针来解决我的基本问题:
So I need some pointers to solve my basic questions:
-
我如何知道原型内容类型的属性名称.例如,在这种情况下,我需要知道新闻项目正文的属性名称.
How can I know the attribute names for Archetype content types. For example, in this case I need to know the name of the attribute for the body text of news item.
如何将新闻中附加的图像转换为灵巧内容类型的图像.
How can I convert an image attached to a news item to an image attached to a dexterity content type.
推荐答案
-
您可以使用原型"架构中的字段来检索值,在这种情况下,最好是原始值.您传入对象,然后调用
.get()
或.getRaw()
:
schema = news.Schema()
news = schema.getField('text').getRaw(news)
imageField = schema.getField('image')
image = imageField.getRaw(news)
content_type = imageField.getContentType(news)
filename = imageField.getFilename(news)
ImageField.getRaw()
调用返回的对象基本上是一个OFS.Image
实例.您可以调用str()
来获取原始图像数据.
The object returned by the ImageField.getRaw()
call is basically a OFS.Image
instance. You can call str()
on it to get the raw image data.
要设置图像对象,您真的想从架构中获取图像字段并将其._type
属性用作工厂:
To set the image object, you really want to get the image field from the schema and use it's ._type
attribute as a factory:
project.image = IProjectInterface.image._type(str(image),
contentType=content_type, filename=filename)
这里的内容类型是可选的; NamedImage
和NamedBlobImage
类型也会自动嗅出内容类型.
The content type here is optional; the NamedImage
and NamedBlobImage
types sniff out the content type automatically too.
这篇关于将原型新闻项迁移到敏捷内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!