本文整理汇总了Python中rest_framework.settings.api_settings.NON_FIELD_ERRORS_KEY属性的典型用法代码示例。如果您正苦于以下问题:Python api_settings.NON_FIELD_ERRORS_KEY属性的具体用法?Python api_settings.NON_FIELD_ERRORS_KEY怎么用?Python api_settings.NON_FIELD_ERRORS_KEY使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类rest_framework.settings.api_settings
的用法示例。
在下文中一共展示了api_settings.NON_FIELD_ERRORS_KEY属性的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_validators
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def run_validators(validators: Iterable[Validator], value: Any) -> None:
fields_errors = OrderedDict() # type: Dict[str, Any]
non_field_errors = [] # type: List[Any]
for validator in validators:
try:
validator(value)
except ValidationError as exc:
if isinstance(exc.detail, Mapping):
for field_name, field_errors in exc.detail.items():
fields_errors.setdefault(field_name, []).extend(
field_errors)
elif isinstance(exc.detail, list):
non_field_errors.extend(exc.detail)
if fields_errors:
errors = {}
errors.update(fields_errors)
errors.setdefault(
api_settings.NON_FIELD_ERRORS_KEY, []).extend(non_field_errors)
raise ValidationError(errors)
if non_field_errors:
# TODO: Issue #109 - remove type: ignore
raise ValidationError(non_field_errors) # type: ignore
示例2: _get_response_json_from_drf_errors
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def _get_response_json_from_drf_errors(self, serializer_errors=None):
if serializer_errors is None:
serializer_errors = {}
if type(serializer_errors) is list:
serializer_errors = {
api_settings.NON_FIELD_ERRORS_KEY: serializer_errors
}
list_of_errors = self._get_list_of_errors(errors_dict=serializer_errors)
response_data = {
self.ERRORS: list_of_errors
}
return response_data
示例3: to_internal_value
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def to_internal_value(self, data):
"""
This implements the same relevant logic as ListSerializer except that if one or more items fail validation,
processing for other items that did not fail will continue.
"""
if not isinstance(data, list):
message = self.error_messages['not_a_list'].format(
input_type=type(data).__name__
)
raise serializers.ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
})
ret = []
for item in data:
try:
validated = self.child.run_validation(item)
except serializers.ValidationError as exc:
ret.append(exc.detail)
else:
ret.append(validated)
return ret
示例4: as_serializer_error
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def as_serializer_error(exc):
assert isinstance(exc, (ValidationError, DjangoValidationError))
if isinstance(exc, DjangoValidationError):
detail = get_error_detail(exc)
else:
detail = exc.detail
if isinstance(detail, Mapping):
# If errors may be a dict we use the standard {key: list of values}.
# Here we ensure that all the values are *lists* of errors.
return {
key: value if isinstance(value, (list, Mapping)) else [value]
for key, value in detail.items()
}
elif isinstance(detail, list):
# Errors raised as a list are non-field errors.
return {
api_settings.NON_FIELD_ERRORS_KEY: detail
}
# Errors raised as a string are non-field errors.
return {
api_settings.NON_FIELD_ERRORS_KEY: [detail]
}
示例5: to_internal_value
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def to_internal_value(self, data):
"""
Dicts of native values <- Dicts of primitive datatypes.
"""
if html.is_html_input(data):
data = html.parse_html_dict(data)
if not isinstance(data, dict):
self.fail('not_a_dict', input_type=type(data).__name__)
if not self.allow_empty and len(data.keys()) == 0:
message = self.error_messages['empty']
raise ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
})
return {
str(key): self.child.run_validation(value)
for key, value in data.items()
}
示例6: _not_found_response
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def _not_found_response(self, key):
errors_key = api_settings.NON_FIELD_ERRORS_KEY
return Response(data={
errors_key: ['Key {} was not found'.format(key)]
}, status=status.HTTP_404_NOT_FOUND)
示例7: to_internal_value
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def to_internal_value(self, data):
"""
This implements the same relevant logic as ListSerializer except that if one or more items fail validation,
processing for other items that did not fail will continue.
"""
if not isinstance(data, list):
message = self.error_messages['not_a_list'].format(
input_type=type(data).__name__
)
raise serializers.ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
})
ret = []
for item in data:
try:
validated = self.child.run_validation(item)
except serializers.ValidationError as exc:
ret.append(
{
'non_field_errors': [{
'code': item.get('code'),
'email': item.get('email'),
'detail': 'failure',
'message': exc.detail['non_field_errors'][0]
}]
}
)
else:
ret.append(validated)
return ret
示例8: django_to_drf_validation_error
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def django_to_drf_validation_error(django_validation_error,
default_errors_key=None):
try:
errors = django_validation_error.message_dict
except AttributeError:
errors = django_validation_error.messages
if default_errors_key:
errors = {default_errors_key: errors}
else:
non_field_errors = errors.pop(NON_FIELD_ERRORS, None)
if non_field_errors:
errors[default_errors_key or api_settings.NON_FIELD_ERRORS_KEY] = non_field_errors
raise serializers.ValidationError(errors)
示例9: to_internal_value
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def to_internal_value(self, data):
"""
Dict of native values <- Dict of primitive datatypes.
"""
if not isinstance(data, Mapping):
message = self.error_messages['invalid'].format(
datatype=type(data).__name__
)
raise ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
}, code='invalid')
ret = OrderedDict()
errors = OrderedDict()
fields = self._writable_fields
for field in fields:
validate_method = getattr(self, 'validate_' + field.field_name, None)
primitive_value = field.get_value(data)
try:
validated_value = field.run_validation(primitive_value)
if validate_method is not None:
validated_value = validate_method(validated_value)
except ValidationError as exc:
errors[field.field_name] = exc.detail
except DjangoValidationError as exc:
errors[field.field_name] = get_error_detail(exc)
except SkipField:
pass
else:
set_value(ret, field.source_attrs, validated_value)
if errors:
raise ValidationError(errors)
return ret
示例10: errors
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def errors(self):
ret = super(Serializer, self).errors
if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
# Edge case. Provide a more descriptive error than
# "this field may not be null", when no data is passed.
detail = ErrorDetail('No data provided', code='null')
ret = {api_settings.NON_FIELD_ERRORS_KEY: [detail]}
return ReturnDict(ret, serializer=self)
# There's some replication of `ListField` here,
# but that's probably better than obfuscating the call hierarchy.
示例11: _get_list_of_errors
# 需要导入模块: from rest_framework.settings import api_settings [as 别名]
# 或者: from rest_framework.settings.api_settings import NON_FIELD_ERRORS_KEY [as 别名]
def _get_list_of_errors(self, field_path='', errors_dict=None):
"""
Error_dict is in the following format:
{
'field1': {
'message': 'some message..'
'code' 'some code...'
},
'field2: ...'
}
"""
if errors_dict is None:
return []
message_value = errors_dict.get(self.MESSAGE, None)
# Note: If 'message' is name of a field we don't want to stop the recursion here!
if message_value is not None and\
(type(message_value) in {str, exceptions.ErrorDetail}):
if field_path:
errors_dict[self.FIELD] = field_path
return [errors_dict]
errors_list = []
for key, value in errors_dict.items():
new_field_path = '{0}.{1}'.format(field_path, key) if field_path else key
key_is_non_field_errors = key == api_settings.NON_FIELD_ERRORS_KEY
if type(value) is list:
current_level_error_list = []
new_value = value
for error in new_value:
# if the type of field_error is list we need to unpack it
field_error = self._unpack(error)
if not key_is_non_field_errors:
field_error[self.FIELD] = new_field_path
current_level_error_list.append(field_error)
else:
path = field_path if key_is_non_field_errors else new_field_path
current_level_error_list = self._get_list_of_errors(field_path=path, errors_dict=value)
errors_list += current_level_error_list
return errors_list