Python django.forms 模块,MultiWidget() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用django.forms.MultiWidget()

项目:django-localized-fields    作者:SectorLabs    | 项目源码 | 文件源码
def get_context(self, name, value, attrs):
        context = super(forms.MultiWidget, self).get_context(name, value, attrs)
        if self.is_localized:
            for widget in self.widgets:
                widget.is_localized = self.is_localized
        # value is a list of values, each corresponding to a widget
        # in self.widgets.
        if not isinstance(value, list):
            value = self.decompress(value)

        final_attrs = context['widget']['attrs']
        input_type = final_attrs.pop('type', None)
        id_ = final_attrs.get('id')
        subwidgets = []
        for i, widget in enumerate(self.widgets):
            if input_type is not None:
                widget.input_type = input_type
            widget_name = '%s_%s' % (name, i)
            try:
                widget_value = value[i]
            except IndexError:
                widget_value = None
            if id_:
                widget_attrs = final_attrs.copy()
                widget_attrs['id'] = '%s_%s' % (id_, i)
            else:
                widget_attrs = final_attrs
            widget_attrs = self.build_widget_attrs(widget, widget_value, widget_attrs)
            widget_context = widget.get_context(widget_name, widget_value, widget_attrs)['widget']
            widget_context.update(dict(
                lang_code=widget.lang_code,
                lang_name=widget.lang_name
            ))
            subwidgets.append(widget_context)
        context['widget']['subwidgets'] = subwidgets
        return context