简体   繁体   English

如何从它的字符串引用中检索python对象

[英]How to retrieve python object from it's string reference

I'm using Deform widgets to display an object's attribute. 我正在使用Deform小部件来显示对象的属性。 In my widget serialization method I can see that my cstruct is correct: 在我的小部件序列化方法中,我可以看到我的cstruct是正确的:

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

When trying to display within jinja2 template the objet's property eg 'created_at' the result is: 当试图在jinja2模板中显示objet的属性,例如'created_at',结果是:

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

What can I do to have the Assessment object considered as object and not as string within Jinja2? 如何将Assessment对象视为对象而不是Jinja2中的字符串? I believe it's very simple but I still can't find out what is wrong. 我相信这很简单,但我仍然无法找出问题所在。 Please help. 请帮忙。

A bit more code: Schema node: 多一点代码:Schema节点:

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

[...] [...]

Widget: 小工具:

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)

It was an error withi this certain Colader version. 这个特定的Colader版本是错误的。 It was forcing the widget to behave singleton-like. 它迫使小部件表现得像单身一样。 Thank you for all your hints! 谢谢你的所有提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM