On a plone site I am working on, I have a form that is used to edit records of objects stored mapped to a table in the backend database. In the interface class, one of the fields is a schema.Datetime field.
class ICalibration(Interface):
"""Interface class for calibration records
"""
...
Last_Calibration = schema.Datetime(title=u"Last Calibration"
description=u"date of last calibration",
)
...
In my updateWidgets function, in the edit form, I try to set the value of the widget,
class EditCalibration(form.Form):
grok.name('edit-calibration')
grok.require('zope2.View')
grok.context(ISiteRoot)
def updateWidgets(self):
super(EditCalibration, self).updateWidgets()
id = self.request.get('id',None)
if id:
currentCal = session.query(Calibration).filter(Calibration.Calibration_ID == id).one()
...
self.widgets["Last_Calibration"].value = currentCal.Last_Calibration
...
but I get this error:
"TypeError: 'datetime.datetime' object has no attribute 'getitem'.
I did try some things that were sort of interesting.
I printed the value of cal.Last_Calibration and its coming out as the date I put in when I added the record.
I tried printing the type of object that cal.Last_Calibration was and it was python's datetime (as opposed to zope's I believe?).
I tried setting the field equal to today's date: datetime.today() and got the same error.
Just for kicks, I also tried converting currentCal.Last_Calibration to a string and passing it into the field, although that just put random numbers inside the fields.
For the record, I imported python's datetime as:
from datetime import datetime
Also, adding a record/calibration works fine, so its not an issue with the database or the sqlalchemy schema I am using.
If it's possible, what is the appropriate way of setting the schema field's value in a updateWidgets function?
Should I use a different widget? If so, all I really need for my form is the date. The add/update function will take a datetime object, so I could create a datetime object from the data, regardless of the type I believe.
In the z3c.form framework the following steps happen to get the widget 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:
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: