Zope.Schema/Plone - How can I set the value of a Datetime field in an updateWidget function?

这一生的挚爱 提交于 2019-12-06 07:38:00

In the z3c.form framework the following steps happen to get the widget value:

  1. If the request already has a value (from a previous form submission), it is used.
  2. If form.ignoreContext == False (the default), it calls form.getContent() to get the "content object" that the form is editing (which defaults to form.context).
  3. If the content object provides the schema interface the field came from, the "field value" will be fetched as an attribute of the object. (If it does not provide the schema interface, z3c.form looks for an adapter of the object to the schema interface, and gets the attribute on the adapter. If the content object is a dict then the field value will be fetched as an item of the dict rather than using attribute lookup.)
  4. A "converter" based on the field type and widget type is used to convert the field value into a widget value. The difference is that the field value is the actual stored value, while the widget value is a string serialization of that value.

Your problem is that you are trying to set the widget value to a datetime rather than the serialization of that datetime that is expected by the widget.

I would do what you're trying to do by overriding getContent instead of updateWidgets:

from five import grok
from plone.directives import form

class EditCalibration(form.SchemaForm):
    grok.name('edit-calibration')
    grok.require('zope2.View')
    grok.context(ISiteRoot)

    schema = ICalibration

    def getContent(self):
        id = self.request.get('id', None)
        if id:
            return session.query(Calibration).filter(Calibration.Calibration_ID == id).one()

You will also need to declare that your Calibration class implements the ICalibration interface, so that z3c.form recognizes that it can get Last_Calibration as an attribute of the Calibration instance. That should look something like this:

from zope.interface import implementer

@implementer(ICalibration)
class Calibration(Base):
    etc...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!