本文整理汇总了Python中django.forms.utils.ErrorList.extend方法的典型用法代码示例。如果您正苦于以下问题:Python ErrorList.extend方法的具体用法?Python ErrorList.extend怎么用?Python ErrorList.extend使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.utils.ErrorList
的用法示例。
在下文中一共展示了ErrorList.extend方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def clean(self, value):
clean_data = []
errors = ErrorList()
if not value or isinstance(value, (list, tuple)):
if not value or not [v for v in value if v not in self.empty_values]:
if self.required:
raise ValidationError(self.error_messages['required'])
else:
return []
else:
raise ValidationError(self.error_messages['invalid'])
for field_value in value:
try:
clean_data.append(self.contained_field.clean(field_value))
except ValidationError as e:
# Collect all validation errors in a single list, which we'll
# raise at the end of clean(), rather than raising a single
# exception for the first error we encounter.
errors.extend(e.messages)
if self.contained_field.required:
self.contained_field.required = False
if errors:
raise ValidationError(errors)
self.validate(clean_data)
self.run_validators(clean_data)
return clean_data
示例2: clean
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def clean(self, value):
"""
We clean every subwidget.
"""
clean_data = []
errors = ErrorList()
is_empty = not value or not [v for v in value if v not in self.empty_values]
if is_empty and not self.required:
return []
if is_empty and self.required:
raise ValidationError(self.error_messages['required'])
if value and not isinstance(value, (list, tuple)):
raise ValidationError(self.error_messages['invalid'])
for field_value, checkbox_value in value:
try:
clean_data.append([self.contained_field.clean(field_value), checkbox_value])
except ValidationError as e:
errors.extend(e.messages)
# FIXME: copy paste from above
if self.contained_field.required:
self.contained_field.required = False
if errors:
raise ValidationError(errors)
self.validate(clean_data)
self.run_validators(clean_data)
return clean_data
示例3: get_error_titles
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def get_error_titles(self):
errors = ErrorList()
for form in self.formsets:
for item in form.errors:
if item:
errors.extend(item)
return errors
示例4: clean
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def clean(self, value):
"""
Validates every value in the given list. A value is validated against
the corresponding Field in self.fields.
For example, if this MultiValueField was instantiated with
fields=(DateField(), TimeField()), clean() would call
DateField.clean(value[0]) and TimeField.clean(value[1]).
"""
clean_data = []
errors = ErrorList()
if not value or isinstance(value, (list, tuple)):
if not value or not [v for v in value if v not in self.empty_values]:
if self.required:
raise ValidationError(self.error_messages['required'], code='required')
else:
return self.compress([])
else:
raise ValidationError(self.error_messages['invalid'], code='invalid')
for i, field in enumerate(self.fields):
try:
field_value = value[i]
except IndexError:
field_value = None
if field_value in self.empty_values:
if self.require_all_fields:
# Raise a 'required' error if the MultiValueField is
# required and any field is empty.
if self.required:
raise ValidationError(self.error_messages['required'], code='required')
elif field.required:
# Otherwise, add an 'incomplete' error to the list of
# collected errors and skip field cleaning, if a required
# field is empty.
if field.error_messages['incomplete'] not in errors:
errors.append(field.error_messages['incomplete'])
continue
try:
clean_data.append(field.clean(field_value))
except ValidationError as e:
# Collect all validation errors in a single list, which we'll
# raise at the end of clean(), rather than raising a single
# exception for the first error we encounter. Skip duplicates.
errors.extend(m for m in e.error_list if m not in errors)
if errors:
raise ValidationError(errors)
out = self.compress(clean_data)
self.validate(out)
self.run_validators(out)
return out
示例5: post
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def post(self, request, *args, **kwargs):
if self.async_response is not None:
return self.async_response
if self.subscription_form.is_valid():
try:
subscription = self.subscription_form.create_subscription()
return HttpResponseRedirect(
reverse(EditSubscriptionView.urlname, args=(subscription.id,))
)
except NewSubscriptionError as e:
errors = ErrorList()
errors.extend([e.message])
self.subscription_form._errors.setdefault(NON_FIELD_ERRORS, errors)
return self.get(request, *args, **kwargs)
示例6: clean
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def clean(self, value):
clean_data = {}
errors = ErrorList()
if not value or isinstance(value, dict):
if not value or not [
v for v in value.values() if v not in self.empty_values
]:
if self.required:
raise ValidationError(self.error_messages['required'])
else:
return {}
else:
raise ValidationError(self.error_messages['invalid'])
# sort out required => at least one element must be in there
for key, val in value.items():
# ignore empties. Can they even come up here?
if key in self.empty_values and val in self.empty_values:
continue
try:
val = self.contained_field.clean(val)
except ValidationError as e:
# Collect all validation errors in a single list, which we'll
# raise at the end of clean(), rather than raising a single
# exception for the first error we encounter.
errors.extend(e.messages)
try:
self._validate_key(key)
except ValidationError as e:
# Collect all validation errors in a single list, which we'll
# raise at the end of clean(), rather than raising a single
# exception for the first error we encounter.
errors.extend(e.messages)
clean_data[key] = val
if self.contained_field.required:
self.contained_field.required = False
if errors:
raise ValidationError(errors)
self.validate(clean_data)
self.run_validators(clean_data)
return clean_data
示例7: clean
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def clean(self, value):
"""
This is a copy of MultiValueField.clean() with a BUGFIX:
- if self.required and field_value in validators.EMPTY_VALUES:
+ if field.required and field_value in validators.EMPTY_VALUES:
"""
try:
from django.forms.utils import ErrorList
except ImportError:
from django.forms.util import ErrorList
from django.core import validators
from django.core.exceptions import ValidationError
clean_data = []
errors = ErrorList()
if not value or isinstance(value, (list, tuple)):
if not value or not [v for v in value if v not in validators.EMPTY_VALUES]:
if self.required:
raise ValidationError(self.error_messages['required'])
else:
return self.compress(value)
else:
raise ValidationError(self.error_messages['invalid'])
for i, field in enumerate(self.fields):
try:
field_value = value[i]
except IndexError:
field_value = None
if field.required and field_value in validators.EMPTY_VALUES:
raise ValidationError(self.error_messages['required'])
try:
clean_data.append(field.clean(field_value))
except ValidationError as e:
# Collect all validation errors in a single list, which we'll
# raise at the end of clean(), rather than raising a single
# exception for the first error we encounter.
errors.extend(e.messages)
if errors:
raise ValidationError(errors)
out = self.compress(clean_data)
self.validate(out)
self.run_validators(out)
return out
示例8: clean
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def clean(self, value):
# Get the value
# Totally replaced validation.
clean_data = []
errors = ErrorList()
# Only the visible field is required.
radio_value = value[0]
field_visible = [False] * len(self.fields)
field_visible[0] = True
if radio_value is None:
# radio_value is None when models are deleted in formsets
out = ''
else:
field_visible[self.url_type_registry.index(radio_value) + 1] = True
# The validators only fire for visible fields.
for i, field in enumerate(self.fields):
try:
field_value = value[i]
except IndexError:
field_value = None
if not field_visible[i]:
clean_data.append(None)
continue
if self.required and field_value in validators.EMPTY_VALUES:
raise ValidationError(self.error_messages['required'])
try:
clean_data.append(field.clean(field_value))
except ValidationError as e:
errors.extend(e.messages) # Collect all widget errors
if errors:
raise ValidationError(errors)
out = self.compress(clean_data)
self.validate(out)
return out
示例9: hidden_field_errors
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def hidden_field_errors(form):
hidden_field_errors = ErrorList()
for field in form.hidden_fields():
hidden_field_errors.extend(field.errors)
return hidden_field_errors
示例10: bulk_view
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def bulk_view(self, request, form_url='', extra_context=None):
to_field = request.POST.get(TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR))
if to_field and not self.to_field_allowed(request, to_field):
raise DisallowedModelAdminToField("The field %s cannot be referenced." % to_field)
model = self.model
opts = model._meta
continue_requested = request.POST.get('_continue', request.GET.get('_continue'))
force_continue = False
inline = self.get_bulk_inline(request)
formset_class = inline.get_formset(request)
formset_params = {}
prefix = formset_class.get_default_prefix()
queryset = inline.get_queryset(request)
if not self.has_add_permission(request):
formset_class.max_num = 0
if request.method == 'GET':
if 'pks' in request.GET and self.has_change_permission(request):
pks = [opts.pk.to_python(pk) for pk in request.GET.get('pks').split(',')]
queryset = queryset.filter(pk__in=pks)
else:
queryset = queryset.none()
elif request.method == 'POST':
management_form = ManagementForm(request.POST, prefix=prefix)
if not management_form.is_valid():
raise ValidationError(
_('ManagementForm data is missing or has been tampered with'),
code='missing_management_form',
)
if not self.has_add_permission(request) and management_form.cleaned_data[INITIAL_FORM_COUNT] < management_form.cleaned_data[TOTAL_FORM_COUNT]:
raise PermissionDenied
if not self.has_change_permission(request) and management_form.cleaned_data[INITIAL_FORM_COUNT] > 0:
raise PermissionDenied
queryset = self.transform_queryset(request, queryset, management_form, prefix)
post, files, force_continue = self.transform_post_and_files(request, prefix)
formset_params.update({
'data': post,
'files': files,
})
formset_params['queryset'] = queryset
formset = formset_class(**formset_params)
if request.method == 'POST':
if formset.is_valid():
self.save_formset(request, form=None, formset=formset, change=False)
if continue_requested or force_continue:
# The implementation of ModelAdmin redirects to the change view if valid and continue was requested
# The change view then reads the edited model again from database
# In our case, we can't make a redirect as we would loose the information which models should be edited
# Thus, we create a new formset with the edited models and continue as this would have been a usual GET request
if self.has_change_permission(request):
queryset = _ListQueryset(queryset)
queryset.extend(formset.new_objects)
else:
queryset = _ListQueryset()
formset_params.update({
'data': None,
'files': None,
'queryset': queryset,
})
formset = formset_class(**formset_params)
msg = _('The %s were bulk added successfully. You may edit them again below.') % (force_text(opts.verbose_name_plural),)
self.message_user(request, msg, messages.SUCCESS)
else:
return self.response_bulk(request, formset)
media = self.media
inline_formsets = self.get_inline_formsets(request, [formset], [inline], obj=None)
for inline_formset in inline_formsets:
media = media + inline_formset.media
errors = ErrorList()
if formset.is_bound:
errors.extend(formset.non_form_errors())
for formset_errors in formset.errors:
errors.extend(list(six.itervalues(formset_errors)))
context = dict(
self.admin_site.each_context(request) if django.VERSION >= (1, 8) else self.admin_site.each_context(),
bulk=True,
bulk_formset_prefix=prefix,
#.........这里部分代码省略.........
示例11: get_error_list
# 需要导入模块: from django.forms.utils import ErrorList [as 别名]
# 或者: from django.forms.utils.ErrorList import extend [as 别名]
def get_error_list(self):
errors = ErrorList()
if self.form_obj.is_bound:
errors.extend(self.form_obj.errors.values())
return errors