Zope.Schema/Plone-如何在updateWidget函数中设置Datetime字段的值? [英] Zope.Schema/Plone - How can I set the value of a Datetime field in an updateWidget function?

查看:106
本文介绍了Zope.Schema/Plone-如何在updateWidget函数中设置Datetime字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在工作的plone站点上,我有一个表单,该表单用于编辑映射到后端数据库中的表的存储对象的记录.在接口类中,字段之一是schema.Datetime字段.

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",


                                      )
...

在我的updateWidgets函数的编辑表单中,我尝试设置窗口小部件的值,

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
        ...

但我收到此错误:

"TypeError:'datetime.datetime'对象没有属性' getitem '.

"TypeError: 'datetime.datetime' object has no attribute 'getitem'.

我确实尝试了一些有趣的事情. 我打印了cal.Last_Calibration的值并将其显示为添加记录时输入的日期.
我尝试打印cal.Last_Calibration是的对象类型,它是python的日期时间(与我相信的zope相反). 我尝试将字段设置为等于今天的日期:datetime.today(),并得到了相同的错误. 只是为了踢球,我还尝试将currentCal.Last_Calibration转换为字符串并将其传递到字段中,尽管这只是在字段中放入了随机数.

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.

为了记录,我将python的日期时间导入为:

For the record, I imported python's datetime as:

from datetime import datetime

此外,添加记录/校准也可以,因此与我正在使用的数据库或sqlalchemy模式无关.

Also, adding a record/calibration works fine, so its not an issue with the database or the sqlalchemy schema I am using.

如果可能,在updateWidgets函数中设置架构字段值的适当方法是什么?

If it's possible, what is the appropriate way of setting the schema field's value in a updateWidgets function?

我应该使用其他小部件吗?如果是这样,我真正需要的只是日期.添加/更新函数将使用一个datetime对象,因此无论我相信哪种类型,我都可以从数据中创建一个datetime对象.

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.

推荐答案

在z3c.form框架中,发生以下步骤来获取小部件值:

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

  1. 如果请求已具有值(来自先前的表单提交),则会使用该值.
  2. 如果form.ignoreContext == False(默认值),它将调用form.getContent()以获取表单正在编辑的内容对象"(默认为form.context).
  3. 如果内容对象提供了字段所属的架构接口,则将字段值"作为对象的属性来获取. (如果z3c.form不提供架构接口,则在架构接口中查找对象的适配器,并在适配器上获取属性.如果内容对象是dict,则将字段值作为项来获取而不是使用属性查找.)
  4. 基于字段类型和窗口小部件类型的转换器"用于将字段值转换为窗口小部件值.区别在于字段值是实际存储的值,而小部件值是该值的字符串序列化.

您的问题是您试图将窗口小部件的值设置为日期时间,而不是将窗口小部件期望的日期时间序列化.

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.

我会通过重写getContent而不是updateWidgets来做您要做的事情:

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()

您还需要声明您的Calibration类实现ICalibration接口,以便z3c.form识别它可以获取Last_Calibration作为Calibration实例的属性.看起来应该像这样:

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...

这篇关于Zope.Schema/Plone-如何在updateWidget函数中设置Datetime字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆