How do I set the RichText field in a content type based on ZMI Dexterity?

I tried to create a collect.nitf.content object using Script (Python) in ZMI.

The code I used:

from Products.CMFCore.utils import getToolByName

news_folder = context.test_folder
wf = getToolByName(news_folder, "portal_workflow")
id="test_news"

news_folder.invokeFactory('collective.nitf.content', id)

n = news_folder[id]

n.setTitle('Test went OK')
n.setText('The test went OK.')
n.indexObject()
wf.doActionFor(n, "publish")

      

When I call n.setTitle()

, the object is created OK, but when I call n.setText()

, this error comes up:

Traceback (innermost last):
  Module ZPublisher.Publish, line 138, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 48, in call_object
  Module Shared.DC.Scripts.Bindings, line 322, in __call__
  Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 28, in mais_teste
   - <PythonScript at /plone/news_folder/test_script>
   - Line 28
AttributeError: setText

      

What am I doing wrong? I researched the days and couldn't find an answer to this question.

Versions:

  • Plone 4.3.3 (4308)
  • CMF 2.2.7
  • Zope 2.13.22
  • Python 2.7.9 (default Apr 9 2015 14:50:13) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]
  • PIL 2.3.0 (Pillow)
+3


source to share


1 answer


collect.nitf is a Dexterity based content type, so there is no need to use setters here, you must assign a value directly to the field.

Also note that the field text

is RichText

; you should use RichTextValue

:



from plone.app.textfield.value import RichTextValue

n.text = RichTextValue(u'The body.', 'text/plain', 'text/html')

      

Take a look at the tests, in particular the test_catalog module .

+4


source







All Articles