本文整理匯總了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)
示例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