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


Python ValidationError.items方法代码示例

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


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

示例1: add_error

# 需要导入模块: from django.core.exceptions import ValidationError [as 别名]
# 或者: from django.core.exceptions.ValidationError import items [as 别名]
    def add_error(self, field, error):
        """
        Update the content of `self._errors`.

        The `field` argument is the name of the field to which the errors
        should be added. If its value is None the errors will be treated as
        NON_FIELD_ERRORS.

        The `error` argument can be a single error, a list of errors, or a
        dictionary that maps field names to lists of errors. What we define as
        an "error" can be either a simple string or an instance of
        ValidationError with its message attribute set and what we define as
        list or dictionary can be an actual `list` or `dict` or an instance
        of ValidationError with its `error_list` or `error_dict` attribute set.

        If `error` is a dictionary, the `field` argument *must* be None and
        errors will be added to the fields that correspond to the keys of the
        dictionary.
        """
        if not isinstance(error, ValidationError):
            # Normalize to ValidationError and let its constructor
            # do the hard work of making sense of the input.
            error = ValidationError(error)

        if hasattr(error, 'error_dict'):
            if field is not None:
                raise TypeError(
                    "The argument `field` must be `None` when the `error` "
                    "argument contains errors for multiple fields."
                )
            else:
                error = error.error_dict
        else:
            error = {field or NON_FIELD_ERRORS: error.error_list}

        for field, error_list in error.items():
            if field not in self.errors:
                if field != NON_FIELD_ERRORS and field not in self.fields:
                    raise ValueError(
                        "'%s' has no field named '%s'." % (self.__class__.__name__, field))
                if field == NON_FIELD_ERRORS:
                    self._errors[field] = self.error_class(error_class='nonfield')
                else:
                    self._errors[field] = self.error_class()
            self._errors[field].extend(error_list)
            if field in self.cleaned_data:
                del self.cleaned_data[field]
开发者ID:zkanda,项目名称:django,代码行数:49,代码来源:forms.py

示例2: add_error

# 需要导入模块: from django.core.exceptions import ValidationError [as 别名]
# 或者: from django.core.exceptions.ValidationError import items [as 别名]
    def add_error(self, field, error):
        """
        Standard django form does not support more structured errors
        (for example error dict that contains another error dict).
        For this purpose pyston creates RESTError class and we must rewrite this method to allow these
        complex error messages.
        """
        if isinstance(error, RESTError):
            if not field:
                raise ValueError('Field must be set for RESTError')
            self._errors[field] = error
        else:
            if not isinstance(error, ValidationError):
                # Normalize to ValidationError and let its constructor
                # do the hard work of making sense of the input.
                error = ValidationError(error)

            if hasattr(error, 'error_dict'):
                if field is not None:
                    raise TypeError(
                        "The argument `field` must be `None` when the `error` "
                        "argument contains errors for multiple fields."
                    )
                else:
                    error = error.error_dict
            else:
                error = {field or NON_FIELD_ERRORS: error.error_list}

            for field, error_list in error.items():
                if field not in self.errors:
                    if field == NON_FIELD_ERRORS:
                        self._errors[field] = self.error_class(error_class='nonfield')
                    else:
                        self._errors[field] = self.error_class()
                self._errors[field].extend(error_list)
                if field in self.cleaned_data:
                    del self.cleaned_data[field]
开发者ID:druids,项目名称:django-pyston,代码行数:39,代码来源:__init__.py

示例3:

# 需要导入模块: from django.core.exceptions import ValidationError [as 别名]
# 或者: from django.core.exceptions.ValidationError import items [as 别名]
"""
开发者ID:letouriste001,项目名称:SmartForest_2.0,代码行数:3,代码来源:forms.py


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