2 Global Django exception and warning classes.
5 class DjangoRuntimeWarning(RuntimeWarning):
8 class ObjectDoesNotExist(Exception):
9 "The requested object does not exist"
10 silent_variable_failure
= True
12 class MultipleObjectsReturned(Exception):
13 "The query returned multiple objects when only one was expected."
16 class SuspiciousOperation(Exception):
17 "The user did something suspicious"
20 class PermissionDenied(Exception):
21 "The user did not have permission to do that"
24 class ViewDoesNotExist(Exception):
25 "The requested view does not exist"
28 class MiddlewareNotUsed(Exception):
29 "This middleware is not used in this server configuration"
32 class ImproperlyConfigured(Exception):
33 "Django is somehow improperly configured"
36 class FieldError(Exception):
37 """Some kind of problem with a model field."""
40 NON_FIELD_ERRORS
= '__all__'
41 class ValidationError(Exception):
42 """An error while validating data."""
43 def __init__(self
, message
, code
=None, params
=None):
45 from django
.utils
.encoding
import force_unicode
47 ValidationError can be passed any object that can be printed (usually
48 a string), a list of objects or a dictionary.
50 if isinstance(message
, dict):
51 self
.message_dict
= message
52 # Reduce each list of messages into a single list.
53 message
= reduce(operator
.add
, message
.values())
55 if isinstance(message
, list):
56 self
.messages
= [force_unicode(msg
) for msg
in message
]
60 message
= force_unicode(message
)
61 self
.messages
= [message
]
64 # This is needed because, without a __str__(), printing an exception
65 # instance would result in this:
66 # AttributeError: ValidationError instance has no attribute 'args'
67 # See http://www.python.org/doc/current/tut/node10.html#handling
68 if hasattr(self
, 'message_dict'):
69 return repr(self
.message_dict
)
70 return repr(self
.messages
)
73 if hasattr(self
, 'message_dict'):
74 return 'ValidationError(%s)' % repr(self
.message_dict
)
75 return 'ValidationError(%s)' % repr(self
.messages
)
77 def update_error_dict(self
, error_dict
):
78 if hasattr(self
, 'message_dict'):
80 for k
, v
in self
.message_dict
.items():
81 error_dict
.setdefault(k
, []).extend(v
)
83 error_dict
= self
.message_dict
85 error_dict
[NON_FIELD_ERRORS
] = self
.messages