1

我正在使用变形小部件来显示对象的属性。在我的小部件序列化方法中,我可以看到我的cstruct是正确的:

serialization cstruct: [<abc.modules.core.models.Assessment object at 0x105583320>]

当试图在 jinja2 模板中显示对象的属性时,例如“created_at”,结果是:

jinja2.exceptions.UndefinedError: 'str object' has no attribute 'created_at'

我该怎么做才能将评估对象视为对象而不是 Jinja2 中的字符串?我相信这很简单,但我仍然无法找出问题所在。请帮忙。

更多代码:模式节点:

class SubcontractorAssessmentSchema(MappingSchema):
    """ **Schema definition for ``Subcontractor Assessment`` part.** """
    readonly = True
    assessments = SchemaNode(String(), widget=LastResultsWidget2(),
                             title='Assessment:', missing='')

[...]

小部件:

class LastResultsWidget2(SelectWidget):
    """ **Widget for Last Results.** """
    template = 'last_results'
    readonly_template = 'last_results'
    strip = True

    def serialize(self, field, cstruct, **kw):
        """
        Overwritten default serialize of SelectWidget.

        :param field: Field
        :param cstruct: Value of Widget
        :param kw: Additional arguments.
        :return: rendered field.
        """
        if cstruct in (colander.null, None):
            cstruct = self.null_value
#        print ('serialization cstruct: ' +str(cstruct))
        return field.renderer(self.template, field=field, cstruct=cstruct)
4

1 回答 1

0

这是某个 Colader 版本的错误。它迫使小部件表现得像单例一样。感谢您的所有提示!

于 2017-04-12T18:10:29.367 回答