当前位置: 首页>>代码示例>>Python>>正文


Python forms.MultiValueField方法代码示例

本文整理汇总了Python中django.forms.MultiValueField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.MultiValueField方法的具体用法?Python forms.MultiValueField怎么用?Python forms.MultiValueField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms的用法示例。


在下文中一共展示了forms.MultiValueField方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import MultiValueField [as 别名]
def __init__(self, field_items, skip_check=False, *args, **kwargs):
        self.field_dict = OrderedDict(field_items)
        self.skip_check = skip_check
        # Make sure no subfield is named 'SKIP_CHECK_NAME'. If
        # skip_check is True this field will clash with the addtional
        # subfield added by the DictCharField constructor.  We perform
        # this check even if skip_check=False because having a field named
        # 'skip_check' that isn't used to actually skip the checks would be
        # very confusing.
        if SKIP_CHECK_NAME in self.field_dict:
            raise RuntimeError(
                "'%s' is a reserved name "
                "(it can't be used to name a subfield)." % SKIP_CHECK_NAME
            )
        # if skip_check: add a BooleanField to the list of fields, this will
        # be used to skip the validation of the fields and accept arbitrary
        # data.
        if skip_check:
            self.field_dict[SKIP_CHECK_NAME] = forms.BooleanField(
                required=False
            )
        self.names = [name for name in self.field_dict]
        # Create the DictCharWidget with init values from the list of fields.
        self.fields = list(self.field_dict.values())
        self.widget = DictCharWidget(
            [field.widget for field in self.fields],
            self.names,
            [field.initial for field in self.fields],
            [field.label for field in self.fields],
            skip_check=skip_check,
        )
        # Upcall to Field and not MultiValueField to avoid setting all the
        # subfields' 'required' attributes to False.
        Field.__init__(self, *args, **kwargs) 
开发者ID:maas,项目名称:maas,代码行数:36,代码来源:config_forms.py

示例2: fromPostData

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import MultiValueField [as 别名]
def fromPostData(self, post_data, files_data, ignore_required=False):
        self.cleaned_data = {}
        for name, field in list(self.fields.items()):
            try:
                if isinstance(field, forms.MultiValueField):
                    relevant_data = dict([(k,v) for k,v in list(post_data.items()) if k.startswith(str(name)+"_")])
                    relevant_data[str(name)] = ''
                    relevant_data['required'] = ignore_required
                    cleaned_data = field.compress(relevant_data)
                elif isinstance(field, forms.FileField):
                    if str(name) in files_data:
                        cleaned_data = field.clean(files_data[str(name)])
                    elif field.filesub:
                        # we have no new file, but an old file submission: fake it into place
                        fs = field.filesub
                        cleaned_data = SimpleUploadedFile(name=fs.file_attachment.name,
                                            content=fs.file_attachment.read(),
                                            content_type=fs.file_mediatype)
                    elif ignore_required:
                        cleaned_data = ""
                    else:
                        cleaned_data = field.clean("")
                elif str(name) in post_data:
                    if ignore_required and post_data[str(name)] == "":
                        cleaned_data = ""
                    else:
                        if isinstance(field, MultipleChoiceField):
                            relevant_data = post_data.getlist(str(name))
                            cleaned_data = field.clean(relevant_data)
                        else:
                            cleaned_data = field.clean(post_data[str(name)])
                else:
                    if ignore_required:
                        cleaned_data = ""
                    else:
                        cleaned_data = field.clean("")
                self.cleaned_data[str(name)] = cleaned_data
                field.initial = cleaned_data
            except forms.ValidationError as e:
                #self.errors[name] = ", ".join(e.messages)
                self.errors[name] = ErrorList(e.messages)
                if str(name) in post_data:
                    field.initial = post_data[str(name)]
                else:
                    initial_data = [v for k,v in list(post_data.items()) if k.startswith(str(name)+"_") and v != '']
                    field.initial = initial_data 
开发者ID:sfu-fas,项目名称:coursys,代码行数:48,代码来源:forms.py


注:本文中的django.forms.MultiValueField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。