【问题标题】:Migrating Archetype news items to Dexterity content types将 Archetype 新闻项目迁移到 Dexterity 内容类型
【发布时间】:2013-03-19 12:31:56
【问题描述】:

我正在尝试将新闻项目中的内容复制到我编写的其他内容类型。在我的脚本中,我有 news 项目和 project 项目。第二个,project,是使用 Dexterity 定义的内容类型。如果我能用下一种方式将图片和正文从news复制到project,那就太好了。

project.text = news.text
project.image = news.image

其中文本和图像在 project 架构中定义为 RichText 和 NamedBlobImage。我不知道新闻项目中的属性如何。我只知道可以使用getImage()的方法获取news item中的图片但是将其分配给项目会在渲染项目时产生错误。

所以我需要一些指针来解决我的基本问题:

  1. 我如何知道 Archetype 内容类型的属性名称。例如,在这种情况下,我需要知道新闻正文的属性名称。

  2. 如何将附加到新闻项目的图像转换为附加到灵巧内容类型的图像。

【问题讨论】:

    标签: plone dexterity archetypes


    【解决方案1】:
    1. 您使用 Archetypes 模式中的字段来检索值,在这种情况下最好是原始值。你传入对象然后调用.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)
      
    2. ImageField.getRaw() 调用返回的对象基本上是一个OFS.Image 实例。您可以在其上调用str() 获取原始图像数据。

      设置图像对象,您真的想从架构中获取图像字段并将其._type 属性用作工厂:

      project.image = IProjectInterface.image._type(str(image),
          contentType=content_type, filename=filename)
      

      这里的内容类型是可选的; NamedImageNamedBlobImage 类型也会自动嗅出内容类型。

    【讨论】:

    • Dank je vel, Martijn!
    猜你喜欢
    • 1970-01-01
    • 2021-09-08
    • 2019-07-21
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多