Python django.forms.MultiValueField() Examples

The following are 2 code examples of django.forms.MultiValueField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.forms , or try the search function .
Example #1
Source File: config_forms.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
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) 
Example #2
Source File: forms.py    From coursys with GNU General Public License v3.0 4 votes vote down vote up
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